The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 4 of 4

Thread: reading from text file

  1. #1

    reading from text file

    i have a program that reads a text file line by line. it performs some calculations and prints the file out to the screen and newly created text file. it works swimmingly except for one thing: it skips the first line of text. i cheated and entered a blank line for the first line of code and it works...except it now prints the last line twice. any suggestions are greatly appreciated.
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<fstream>

    using namespace std;

    int main()
    {
    //////////////Input File Check////////////////////////

    bool flag = false;
    fstream bankStatement;
    bankStatement.open("bankAccounts.txt");
    if(bankStatement.is_open() )
    {
    cout << "Opening customer account information..." << endl << endl;
    flag = true;
    }
    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    //////////////Input File Check////////////////////////
    
        bool flag = false;
        fstream bankStatement;
        bankStatement.open("bankAccounts.txt");
        if(bankStatement.is_open() )
        {
            cout << "Opening customer account information..." << endl << endl;
            flag = true;
        }
    .
    .
    .
    string oneLine;
    while (bankStatement)
    {
    string type;
    getline(bankStatement, oneLine);
    bankStatement >> accountNumber >> accountType >> minBalance >> accountBalance;

    .
    .
    .
    }
    bankStatement.close();
    return 0;
    }
    Code:
    string oneLine;
        while (bankStatement)
        {
            string type;
            getline(bankStatement, oneLine);
            bankStatement >> accountNumber >> accountType >> minBalance >> accountBalance;
    
    .
    .
    .
    }
    bankStatement.close();
    return 0;
    }

  2. #2
    Your getline eats a line of your input file. Then you extract account number, type, etc after that. So you basically skip each odd line. Get rid of that line.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  3. #3
    1,839
    posts
    Registered User
    plus afaik you would want while (!infile.eof()) ...although i guess your method works i think its more accepted to use eof...but that doesnt sound like your problem

  4. #4
    it performs some calculations and prints the file out to the screen and newly created text file. it works swimmingly except for one thing: it skips the first line of text. i cheated and entered a blank line for the first line of code and it works...except it now prints the last line twice.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012