how do you use eof?

Hi guys I'm trying to write a program for class and I'm having trouble getting it to read all the values in the input file and outputting them. Sorry about asking this, but the teacher I have is no good and the book only does so much. Also i was wondering if i could use eof too see when it reaches the end of file, but my program says: std::string' has no member named 'eof' when i try it.

Here's my code so far. Tried using the eof in line 27:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a = "fileContainingEmails.txt";
    string b = "copyPasteMyEmails.txt";
    
    //open input file
    ifstream fin;
    string fileName;
    cout<<"Enter input filename [default: fileContainingEmails.txt]: ";
    getline(cin,fileName);
    if((cin,fileName) != "")
    {
                     fileName = fileName;
    }
    else
        fileName = a;
    fin.open(fileName.c_str());
    if (!fin.good()) throw, "I/O error";
    
    while (true) 
  { 
    if(!fileName.eof());
    string lineFromFile; 
    getline(fin, lineFromFile); 
    cout << lineFromFile << endl; 
  
  fin.close();
    
    //open output file
    ofstream fout;
    string fileName1;
    cout<<"Enter input filename [default: copyPasteMyEmails.txt]"<<endl;
    cout<<"[default if value given for input: same file as input]: ";
    getline(cin, fileName1);
    if((cin,fileName1) != "")
    {
                       fileName1 = fileName1;
    }
    else if ((cin,fileName1) == "" && fileName == a)
        fileName1 = b;
    else if ((cin,fileName1) == "" &&  fileName != a)
         fileName1 = fileName;
    fout.open(fileName.c_str());
    if (!fin.good()) throw, "I/O error";
    
    cout<<endl;
    cout<<fileName<<" will be used for input."<<endl;
    cout<<fileName1<<" will be used for output."<<endl;
}
    
    
     
         return 0;
}
          
    
    
    
                 
    
*/
Last edited on
That if statement is completely useless; it says, if we aren't at the end of file, do nothing, then continue as normal.
Few things:

First, on line 25, on your while loop, you have no closing bracket, unless you copy pasted wrong.
Second, instead of a while loop (a pretest loop) you could change to a posttest loop in do/while, and use while(fin.good()).

But to answer your question, the fin.eof() should return false until eof is reached, at which time it will return true.

Why you are getting that error, I think it is because you are trying to use it on a file instead of a stream, I believe it is intended to be used on the stream. Try changing it from fileName.eof to fin.eof()
Strings don't have and eof. Files do. You probably wanted to do some_fstream.eof() instead. In any case, that if statement is useless as it does nothing.

EDIT: Beaten by there. Soko is right, there are several other errors besides that you ought to fix.
Last edited on
Thanks for pointing out those errors guys. I'm really new so sometimes i just throw stuff together and pray it works.
I have been working with c++ for half a year now (still pretty new myself) and with some projects I still do that. I throw it together, and once I have the raw raw draft of it, I fix all my errors then go from there. :)
I know this is solved but the better use for it is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// function process file
void procFile()
{
    string line;
    ifstream inFile("filename.txt");
    if (! inFile.is_open())
    {
        cerr << "Unable to open inFile" << endl;
        exit (1);
    }
    
    while ( ! inFile.eof())
    {
        getline(inFile, line);
        cout << line << endl;
    }
}


edit: changed is_open()
Last edited on
Only problem is if there was an error of some kind you would have a infinite loop. Better to loop on good().
no eof marker? what kind of error?
Topic archived. No new replies allowed.