Fgets () linux vs mac

Does fgets do on the layout until the carriage returns '\ r' or will it also depend on the new line '\ n'?

The reason is because I use fgets to read the file line by line at a time. However, if it is running on a Mac file with only "\ r" to end the line, it does not do what I want. (runs on Linux)

I do not want to write functions like a library to solve cross-compatibility issues. Is there another standard feature that I could use instead?

+3
source share
6 answers

If you strictly adhere to C, you can try using getdelim()if it is available on your system.

+4
source

Oh wait, I missed "run on linux" with "Mac file with CR".

OK, answer: The documentation fgets()explicitly refers to the "new line" as the line terminator. Especially you should not expect that Unix / Linux implementations have heard of the old Mac concept of CR as a line terminator; therefore it is not surprising that they fgetsdo not see these CRs as the end of a line.

Update:

My strong recommendation to avoid most of the problem is to use a command-line utility such as trto simply translate the file before throwing your program into it.

+3

MacOS X MacOS 9 ( )?

  • MacOS 9 "\ r".
  • MacOS X "\n", Unix.
+1

, Mac? Unix? , ? .

TR, CR LF, . . CR , fgets.

+1

Mac Mac OS 9 Mac OS X, , \r \n, Mac OS X BSD, \n . , Mac, fgets , \n, \r.

, , \r, \n.

+1

C Mac " ".

, Linux Mac \n , \r

0

Source: https://habr.com/ru/post/1728390/


All Articles