开发者

How to fix this file related problem

开发者 https://www.devze.com 2022-12-13 11:10 出处:网络
i m reading from file line by line but when i read some garbage character likespace /r is being added i m nt getting why it is being added although there isno such character in file from where i m rea

i m reading from file line by line but when i read some garbage character like space /r is being added i m nt getting why it is being added although there is no such character in file from where i m reading ..i have used fr开发者_如何学Pythonead and fgets both from both i m getting the same problem please reply if u have solution for this problem


The file was probably edited/created on Windows. Windows uses \r\n as a line delimiter. When you read the file, you must strip the \r manually. Since most editors treat \r\n as a single character (line end), you can't "see" it but it's still in the file. Use a hex editor if you want to see it or a tool like od.


Open the file in text mode.

/* ... */
fopen(filename, "r"); /* notice no 'b' in mode */
/* ... */

Supposing you're on Windows ... on reading operations, the library is responsible for translating the literal "\r\n" present on disk to "\n"; and on writing operation, the library translates "\n" to "\r\n".

0

精彩评论

暂无评论...
验证码 换一张
取 消