Home > Net >  CS50 Lab4: CS50 IDE (Codespaces) works correct, while local compilation under Windows 10 fails
CS50 Lab4: CS50 IDE (Codespaces) works correct, while local compilation under Windows 10 fails

Time:04-17

CS50 https://cdn.discordapp.com/attachments/792992622196424725/964848554663362570/unknown.png

but all my local compilers (I've tried: bcc32, cpp32, tcc, gcc, clang) gives the same result - output of this broken file (must be 345kb file, but it's 5 kb): https://cdn.discordapp.com/attachments/662334347163992104/964845484860645426/unknown.png

According debug it always stops at 2117 step (4608 buffer value).

Again I wanna point that in CS50 IDE it works alright and it goest through all 176399 steps :) https://cdn.discordapp.com/attachments/792992622196424725/964844697203912775/unknown.png

feof(input) and ferror() debug: https://cdn.discordapp.com/attachments/792992622196424725/964956659107639346/unknown.png

Please help to solve this puzzle! I can't rest until I understand whats wrong there..

CodePudding user response:

On Windows, you must open binary files with mode rb (or wb for writing). On Unix, you should do that for portability, but in practice it works either way. (And it has nothing to do with the compiler).

The reason is that in text files, Windows treats a byte with value 0x1A (which is Ctl-Z) as an EOF indicator. Unix doesn't do this; on Unix, the end of the file is where the file ends.

Also, Windows uses a two-character end-of-line indicator (\r\n), which must be translated to a single \n because the C standard requires that multi-character end-of-line indicators in a text file be translated to a single newline character (and translated back when you write to the file). That doesn't happen on Unix either, because Unix line endings are already a single newline character.

So on Windows, if you read a binary file without specifying the b-for-binary open mode, then the read will stop at the first 0x1A in the file. In your case, that seems to have happened on the 2117th character read, but note that that might not be the 2117th character in the file because of newline translation. You could try looking at your file with a binary editor, but the bottom line is that if you think your program might be run under Windows, then you should always use rb and wb for binary files. Unix ignores the b and it tells Windows to stop messing with your file.

  • Related