读取以 CR 结尾的关键字文本文件
我想从(文本)文件中读取关键字列表,然后将它们添加到 C 中的 CString 数组中。问题是,我正在逐行读取文件,并且文件每一行都包含一个单词。我可以成功填充数组,但是当我尝试在另一个字符串中查找这些关键字时,它返回 false,因为我猜测关键字末尾有 \n 。
我可以读取文件的另一种方法是,使文本文件成为逗号分隔的文件,然后读取一行并将其标记化。但是,我不知道如何读取大小可能非常大的行,因为关键字列表不断扩展。
萨阿德·雷曼
I want to read a list of keywords from a (text) file and then add those in a CString array in C. The trouble is that, I am reading the file line by line, and the file contains one word in every line. I can successfully populate the array, but when I try to look up these keywords in another string, it returns false because I am guessing the keyword has \n at the end.
Another way I could read the file could be, to make the text file a comma separated file, and read one line and tokenize it. But then, I won't know how to read a line whose size can be VERY large, as the list of keyword is ever expanding.
Saad Rehman
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的问题是字符串末尾可能有恶意换行符,您可以使用:
在读入字符串之后但使用之前对
mystring
执行此操作。它只是检查最后一个字符是否是换行符,如果是,则将其替换为字符串终止符。
第一个检查是确保您不会在空字符串上尝试此操作,其中
mystring[-1]
会调用可怕的未定义行为。If your problem is that a string may have a rogue newline at the end, you can use:
Do this to
mystring
after you've read it in but before you use it.It simply checks if the last character is a newline and, if so, replaces it with a string terminator.
The first check is to ensure you don't try this on an empty string where
mystring[-1]
would invoke the dreaded undefined behaviour.