strtok challenge

S

Sheldon

Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?

Any help is appreciated!

/S
 
J

John Gordon

In said:
I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?

You could include '\n' in strtok's separator string and call strtok an
additional time.

Or you could remove the '\n' when you first read the line from your input,
before you even call strtok.

Generally, any program that deals with line-oriented input needs to decide
if it cares about the newline character at the end of every line. If not,
it's generally best to strip it out immediately after reading the line,
before the line is processed.
 
S

santosh

Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.

A string in C is defined as being terminated by a null character.
Sequences of characters delimited by whitespace do not meet C's
definition of a string.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?

Yes, strtok() can be sufficient for this. Just supply the newline
character in the delimiter string supplied to strtok().
 
J

jason

Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace. After reading each line
and using strtok() to split the string into 3 strings, the last string
is saved with the '\n' character still attached. I would like to know if
strtok can be used to fix this problem or do I have to use strlen and
and if statement to solve this?

Any help is appreciated!

/S

Hello /S use \n in your delimiter array.

Jas.
 
S

Sheldon

You could include '\n' in strtok's separator string and call strtok an
additional time.

Or you could remove the '\n' when you first read the line from your input,
before you even call strtok.

Generally, any program that deals with line-oriented input needs to decide
if it cares about the newline character at the end of every line. If not,
it's generally best to strip it out immediately after reading the line,
before the line is processed.

thanks!
It works with strtok(line, " \n");
/S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top