Search code examples
clinuxwindowsstrlencalloc

different string lengths from Windows vs Linux input from file but not stdin strcmp not working


Question was too long. Here's the solution.

Solution:

When trying to get 'just' the line without any special characters such as \n or \r then loop through the string (char* type) until you hit either the \r or \n so that it is slightly cross-compatible between Windows and other Linux machines.

Possible code:

for (i = displacement; i < strlen(line) && line[i] != '\r' && line[i] != '\n'; i++) {
    newString[i - displacement] = line[i];
}

Solution

  • When reading from a file, fgets reads up to the \n line terminator (which it includes).

    Under Windows, a line read from a text file ends in \r\n.

    Under Unix, a line read from a text file ends in just \n.

    So under Windows, you'd expect the string read by fgets to be 1 byte longer.