简体   繁体   中英

Issues reading from file into a correct “format” using strtok

I am having an issue reading in a file with a certain format using strtok. Each line in the file contains a format as shown below:

Bill Simpson, 01452356

When I display only temp_id I get (Underscore = whitespace):

"_01452356" 

The code for breaking apart each line is as follows:

while((fgets(temp_string, LENGTH, ifp))!= NULL)
    {
          temp_name = strtok(temp_string, comma);
          temp_id = strtok(NULL, comma);
          add(temp_name, temp_id);
    }

I simply want temp_id to not contain whitespace. Keep in mind that temp_name and temp_id are both arrays of type char. I would greatly appreciate a quick solution to this issue.

You need to explicity include the space character as a delimiter.

char comma[] = ", ";

They could be the other way round

char comma[] = " ,";

because they are not a sequence, but a set of chars that may be delimiters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM