文件中 EOF 之前的 \n 字符导致问题
我已手动将一些数据写入文件,即不是通过我的应用程序。
我的代码正在逐个字符地读取数据并将它们存储在不同的数组中,但是当我插入条件 EOF 时,我的程序会卡住。
经过一番调查后,我发现文件中 EOF 之前有三到四个 \n
字符。我还没有插入它们。我不明白为什么它们会出现在我的文件中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想要删除那些讨厌的额外字符吗?首先,查看文件末尾有多少个字符:
然后,删除您不喜欢的字符。如果是 3:
但总的来说,如果是我,我会更改程序以丢弃不需要的换行符,除非根据输入文件格式规范它们确实无效。
Want to remove those pesky extra characters? First, see how many of them there are at the end of your file:
Then, remove however many characters you don't like. If it's 3:
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.
在每个文本编辑器中向文件末尾添加额外的换行符非常容易。您必须推动光标才能看到它们。在编辑器中打开文件,看看导航到末尾时会发生什么,您将看到额外的换行符。
一般来说,不存在 EOF 字符这样的东西。在某些情况下,Windows 将 control-Z 视为 EOF。也许您正在谈论某些 API 的返回值,表明它已到达文件末尾?
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.
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?