Home:ALL Converter>C: strtok and newlines in Windows vs Linux

C: strtok and newlines in Windows vs Linux

Ask Time:2015-03-09T00:37:19         Author:Martijn Courteaux

Json Formatter

I'm working on a C school assignment that is intended to be done on Windows, however, I'm programming it on OS X. While the other students working on Windows don't have problems reading a file, I do.

The code provided by the tutors splits the contents of a file on \n using this code:

/* Read ADFGX information */
adfgx = read_from_file("adfgx.txt");

/* Define the alphabet */
alphabet = strtok(adfgx, "\n");

/* Define the code symbols */
symbols = strtok(NULL, "\n");

However, the file adfgx.txt (which is provided for the assignment) has Windows style newlines (\r\n): I checked it with a hex editor. So, compiling this with the Microsoft C compiler from Visual Studio and running it on Windows splits the file correctly on newlines (\r\n). Which I think is weird, because I can not find any documentation on this behavior. The other part: when I compile it on OS X using gcc, and I run it: the \r is still included in the tokenized string, because it obviously splits on \n. If I change the delimiters to the strtok call to "\r\n", it works for me.

Is this normal that this behaves differently on Windows and Unix? How should I handle this in real life situations (assuming I'm trying to write portable code for Windows and Unix in C that should handle file input that uses \r\n)?

Author:Martijn Courteaux,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28928990/c-strtok-and-newlines-in-windows-vs-linux
yy