hi, well firstly line 10 will only read in 1 element i.e the first thing in words.txt, if you want to read in more you need a loop for that,, secondly your code looks fine to me but you may try combining line 7 and 8 as:
ifstream infile("words.txt", ios::in);
hehehe and as for line 1, the "surplus" iostream, is called a header file and allows a user to display stuff to the console i.e cmd
check out the articles here in this website, i find they are more informative than most textbooks!
now to read in all that gunk in yout words.txt file use this
1 2 3
while(!infile.eof()){
infile>>words;
}
instead of line 10 which only reads in the first element!
This works with your words.txt in my pc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream> //ignore the surplus includes, my textbook hasn't taught...
#include <vector> //...me when i need what yet
#include <cmath>
#include <fstream>
usingnamespace std;
int main()
{
ifstream infile;
infile.open("input.txt");
string data;
while(!infile.eof()) {
infile >> data;
}
infile.close();
cout << data;
return 0;
}
also i do not understand what you mean by the first "thing" will be read. There is no whitespace in the text file so where would the computer draw the line between "things".
hehee, i meant for cases where in there is a whitespace, i hadnt seen your words.txt when i typed that,, your initial program should reads in all the stuff in words.txt 0.o
sometimes you dont. for example: while(getline(some_stream, some_vector[i++]));
where some_vector is of type vector<string> or really any string container
The most likely culprit is that you don't have the text file in the working directory. One way you can determine which directory is the working directory without resorting to compiler specific methods is to create a file and note the location it was created in by inspecting directories after the fact:
1 2 3 4 5 6
#include <fstream>
int main()
{
std::ofstream file("in_working_directory") ;
}
Your text file should be in the same place as the in_working_directory file created thusly.
That said, you should probably also check to make sure the file was opened correctly for input. You do not currently.
@DTSCode: no, I don't know where you read that, that is wrong.
@Ozwurld: with this wrong code there are some problems:
1 2 3 4 5
while(!infile.eof()) //condition is checked first
{
infile >> data; //we try to read data second
//at this point we don't know if data is valid or not
}
This issue is that even if the get pointer is at the end of the file, the EOF flag is not set until you try to read at the end of the file. This means you can read the last character and .eof() is still false! This leads to the last value to be processed being invalid.
it is most certianly not wrong. eof is a character in the fileClosed account? Rage quite?
edit: my mistake. read this and realized i was wrong. ill have to consult my books again and see how old they are http://latedev.wordpress.com/2012/12/04/all-about-eof/
its not an assertion cire. i couldnt find anything in google to disprove me (until i changed the search conditions) and was basing my info on my books from dennis ritchie and brian kerningham and bajarne stroustrop. as i said though, i need to see how old they are
If you cast your input from int to char and try to compare it to the EOF macro, then you will have problems as the cast can cause natural data to compare equal to the EOF constant. Hence we don't read data this way ;)
I would let this slide if it wasn't another example of imprecision immediately following the previous one(s.) Please, use a dictionary before correcting people about the meaning of words.