J Baba J Baba - 3 years ago 195
C Question

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download