[英]Reading a text file that is delimited by a newline. C++ on unix
Ok I have tried many different suggestions on the www, and came up flat with all the examples. 好的,我在www上尝试了许多不同的建议,并给出了所有示例。 This is such a simple thing that is not working for me it is frustrating!
这是一件简单的事情,对我不起作用,令人沮丧! I have attached the file that I need to read into a data structure.
我已将需要读取的文件附加到数据结构中。 In the example I am just printing it to the screen.
在示例中,我只是将其打印到屏幕上。 This following code has worked for me before, but now isn't.
以下代码以前对我有用,但现在不起作用。 I have tried in visual studios, and on unix, and neither are running it.
我曾在Visual Studio和Unix中尝试过,但都没有运行它。 I ultimately need it to run on unix.
我最终需要它在Unix上运行。
Can anyone tell me why this is not working, or suggest a better way to read in my file? 谁能告诉我为什么这不起作用,或者建议一种更好的方式来读取我的文件? Example file:
示例文件:
word
book
programming
Here is my function to read in the file. 这是我读取文件的功能。
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include "words.txt"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(getline(file, line)){
getline(file, line);
cout << line <<endl;
}
}
file.close();
return 0;
}
Thank you. 谢谢。
the file but is throwing errors about delimiters. 文件,但引发有关定界符的错误。 For example in unix running with g++ :
例如,在以g ++运行的unix中:
wordlist2.txt: 498841:1: error: missing terminating ' character
incumbent's
^
wordlist2.txt:498875:13 warning: missing terminating ' character [enabled by default]
at your wits' end
^
And in visual studios: 在视觉工作室中:
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 14 error C2146: syntax error : missing ';' before identifier 'mouse'
while (!file.eof())
Is not a good idea. 不是一个好主意。 Write the following instead:
改为编写以下内容:
while (getline(file, line)) {
cout << line << endl;
}
You certainly don't want to 你当然不想
#include "words.txt"
Remove this line!! 删除此行!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.