Reading files with DOS line endings using fgets() on linux

后端 未结 4 620
小蘑菇
小蘑菇 2021-01-22 16:15

I have a file with DOS line endings that I receive at run-time, so I cannot convert the line endings to UNIX-style offline. Also, my app runs on both Windows and Linux. My app d

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-22 17:05

    Although the other answers gave satisfying information regarind the question what kind of line ending would be returned for a DOS file read under UNIX, I'd like to mentioned an alternative way to chop off such line endings.

    The significant difference is, that the following approach is multi-byte-character save, as it does not involve any characters directly:

    if (pszLine && (2 <= strlen(pszLine)))
    { 
      size_t size = strcspn(pszLine, "\r\n"); 
      pszLine[size] = 0; 
    } 
    

提交回复
热议问题