简体   繁体   中英

Strsep and iCalendar parsing in C

I'm having trouble parsing a simple iCalendar file in C.

char * description, * identifier, *tofree;
tofree = description = strdup(string);
identifier = strsep(&description, ":");
printf("{%s}\n", identifier);
printf("[%s]\n", description);
free(tofree);

string would be the line I just read from the file, such as: BEGIN:VCALENDAR When I run this program, I get the following output:

{BEGIN}
]VCALENDAR

Can someone please help me determine what's causing this issue? The output should show "[VCALENDAR]" and I'm not sure why this is occurring.

Your input data ends with a carriage return, \\r . Your code outputs:

[VCALENDAR\r]\n

and the carriage return places the cursor at the start of the line so the ] overwrites your [ .

It's probably best to remove the line ending before scanning; failing that, include \\r and \\n in the delimiters provided to strsep() , but you'd have to use strsep() a second time to get the VCALENDAR token.

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