Trying to view contents of a file I'm opening

int main()
{
ifstream inputFile;
string name;

inputFile.open("/users/library/resources/americanenglish.txt");

for (name = 1; name > 1; name++);
{
cout << name << endl;
}

inputFile.close();
return 0;
}

I wrote this code to try to view the contents of a library. I can't seem to get the contents to cout. What am I doing wrong? I wrote a for loop to see all the words which should be a couple hundred.

can you post a sample of the text file ? so we can actually know how to read it...

~~~~~~~~~~~

The simplest way to read a file contents would be to use a while loop,

say you text pattern is something like :
 // file "foo.txt"
cat
goat
zebra
dog
...


then to read it :
1
2
3
4
5
6
7
ifstream animals( "foo.txt" );

string name;

// get a single word, store it to name, then print name variable, repeat, until EOF
while( animals >> name )
    cout << name << endl;


EDIT
You can also look at the c++ doc about file i/o :

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>

int main()
{
    const char* const path = "/users/library/resources/americanenglish.txt" ;
    std::cout << std::ifstream(path).rdbuf() ;
}
Thanks guys!
Topic archived. No new replies allowed.