guwop69
guwop69

Reputation: 119

Reading last line in file(File Handling in C++)

I am reading the lines in my .txt file. I got a problem in the last line. It wont read it. I have used eof() which is not a good practice. So I tried using the reading part as the parameters of my loop.

Here it is:

void browse(int accNum)
{
    int acnum,pin;
    float bal;
    string fname,lname;

    ifstream file;
    file.open("acc.txt",ios::in);

    if(file.is_open())
    {
        while(file>>acnum>>pin>>fname>>lname>>bal)
        {
            if(acnum==accNum)
            {
                cout<<"ACCOUNT NUMBER: "<<acnum<<"\nNAME: "<<fname<<" "<<lname<<"\nBALANCE: "<<bal;
                break;
            }
            else
            {
                cout<<"ERROR";
            }

        }
    }
    else
    {
        cout<<"FILE NOT OPEN";
    }
    file.close();
}

I've changed it to while((file >> acnum >> pin >> fname >> lname >> bal) != NULL). It is still not working. What is the solution for this? Here is the content of the database:

   1111111111 4324 James Doe 300000
    2222222222 0123 Eric Doe 10000
    1234567899 1234 John Doe 444444

Upvotes: 1

Views: 904

Answers (4)

moooeeeep
moooeeeep

Reputation: 32502

Your file format (space separated values?) is broken. You can't discriminate the end of a name entry. std::cin will stop at the next whitespace and thus may tear the name apart. Reading the following balance will fail because it's actually still part of the name. There are names containing more whitespace than you'd expect, e.g., Guido van Rossum. You should use ';' or '\t' as a delimiter and use getline and stringstream for reading.

Also, you should not use float to represent money, because of the inherent inaccuracy of the representation. Consider this snippet: (Also have a look at the link I have put below for further reading.)

#include <iostream>
#include <iomanip>
int main() {
    float f = 123456789;
    // prints 123456792.000000
    std::cout << std::fixed << f << '\n';
}

Finally, as a commenter has observed, you need to make sure to use a large enough integer type to hold the account ID. Also consider using an unsigned integer type when you don't expect negative values, e.g., unsigned long long int or uint64_t. Note that life might be easier, when you would ignore the numerical properties of the account ID and just store it as a string instead.

For further reading:

Upvotes: 3

Ziezi
Ziezi

Reputation: 6457

Here is one possible implementation:

// compare against a defined value 
int initilization_value = 10;
int acnum = initilization_value;  
// define  an input stream
ifstream file;
// attach it to the file with input mode (redundant as it is an input stream )
file.open("acc.txt", ios::in);
// check if file open
if(!file) cerr << "FILE NOT OPEN";
// extract input until the end of file
string line;
while(getline(file, line)){ 
    stringstream s(line);
    // in addition to that you can add a loop to differentiate the 
    // values extracted from s, depending on their type 
    // (isdigit(), isalpha(), isspace()), to populate your values 
    // in some special cases (as multiple names etc)
    s >> acnum >> pin >> fname >> lname >> bal;
    // test the input
    if(acnum != accNum){
        cout<<"wrong input variable: "<< anNum << '\n';
    }
    // all OK, exit the while loop  
    cout <<"ACCOUNT NUMBER: "<< acnum <<"\nNAME: "<< fname <<" "<<lname<<"\nBALANCE: "<< bal;
    break;
}

Upvotes: 0

harry
harry

Reputation: 41

I think so you can use,

while(file)
{
   body;
 }

because as soon as you have read everything,there will be nothing in the file to read and so it will return NULL to the while loop and it stops.

Upvotes: 0

kemotoe
kemotoe

Reputation: 1777

It seems your file doesn't have an ending newline i.e. an extra line at the end of file. If you can alter the database and add an extra line the code will work.

If you cannot, you have to have a priming read for your while loop.

file >> acnum >> pin >> fname >> lname >> bal; before you start your loop and the same after your done performing all calculations inside the loop

Upvotes: 0

Related Questions