
-----------------------------------
FeZbOy
Tue Apr 05, 2011 4:24 pm

Understanding fstream
-----------------------------------
I am currently in an introduction to C++ course in college. Currently, they are having us use fstream to read from and write to files. (simple .txt files, nothing major)

My current problem is this. The program reads from a file of names, pay rates, and hours worked. I have a while loop detecting when the stream fails. When it does, the program finishes. However, the last records are duplicated, as if the stream re-reads the last line. Is there a way to prevent this from happening?

Thanks for helping.

-----------------------------------
apython1992
Tue Apr 05, 2011 4:51 pm

RE:Understanding fstream
-----------------------------------
Can you paste your fstream code?

-----------------------------------
unoho
Tue Apr 05, 2011 5:01 pm

RE:Understanding fstream
-----------------------------------
i usually do this:

[code]
ifstream fileName("file.txt");
string input

if(!fileName){
cerrinput){
//do ur code here
}

[/code]

that usually prevents any duplication for last line.

-----------------------------------
FeZbOy
Tue Apr 05, 2011 5:04 pm

Re: Understanding fstream
-----------------------------------
Here is the code.

[code]
string firstName;
    string lastName;
    double hoursWorked;
    double ratePay;
    double grossPay;
    
    bool cont = true;
    
    ifstream inData;
    ofstream outData;
    
    inData.open("hours.txt");
    if (inData.fail())
    {
        cout  firstName;
                inData >> lastName;
                inData >> hoursWorked;
                inData >> ratePay;
                
                cout 