Ok I have tried many different suggestions on the www, and came up flat with all the examples. 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. I ultimately need it to run on 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++ :
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!!