开发者

\n characters before EOF in file causing problems

开发者 https://www.devze.com 2023-02-22 05:42 出处:网络
I have written some data to a file manually i.e. not by my application.开发者_如何转开发 My code is reading the data char by char and storing them in different arrays but my program gets stuck when

I have written some data to a file manually i.e. not by my application.开发者_如何转开发

My code is reading the data char by char and storing them in different arrays but my program gets stuck when I insert the condition EOF.

After some investigation I found out that in my file before EOF there are three to four \n characters. I have not inserted them. I don't understand why they are in my file.


Want to remove those pesky extra characters? First, see how many of them there are at the end of your file:

od -c <filename> | tail

Then, remove however many characters you don't like. If it's 3:

truncate -s -3 <filename>

But overall, if it were me, I'd change my program to discard undesired newline characters, unless they're truly invalid according to the input file format specification.


  1. It is very easy to add additional newlines to the end of a file in every text editor. You have to push the cursor around to see them. Open your file in your editor and see what happens when you navigate to the end, you'll see the extra newlines.

  2. There is no such thing as an EOF character in general. Windows treats control-Z as EOF in some cases. Perhaps you are talking about the return value from some API that indicates that it has reached the end of file?

0

精彩评论

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