CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2011
    Posts
    2

    Reading data from files

    I need some help reading data from a file. I am trying to use the function void checkSpecials(ifstream& infile, ofstream& outfile) to read the file contents and the do calculations using the entries in the file. The file data is structured as follow: 5 3 2 670.00. the first number being the number om people at the table (members) the second being the number of specials (specials), the third being the bottles of wine (wine) and lastly the amount spent(bill).

    If I use the function as it is now all amounts are zero. Is "infile >> members >> specials >> wine >> bill" the correct way to read the data?

    [CODE]#include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    using namespace std;

    void checkSpecials(ifstream& infile)
    {
    string temp;
    cout << endl << "Display orders stored in file orders.dat: " << endl;
    while (getline(infile, temp))
    {
    cout << temp << endl;
    }
    }


    void checkSpecials(ifstream& infile, ofstream& outfile)
    {
    int numSpecials = 0;
    float commision = 0;
    int noServed =0;
    float avgSpent = 0;
    int count = 0;
    double total = 0;
    int value, members, specials, wine;
    float bill;

    infile >> members >> specials >> wine >> bill;

    while(infile >> value)
    {
    noServed ++;
    total += bill;
    avgSpent = ((total / noServed) + (total % noServed));

    if((members >= 5) && (specials >= 4) && (wine >= 2))
    {
    numSpecials ++;

    }

    }

    outfile << "Number of families that ordered the special: " << numSpecials << endl
    << "Commision earned from the special meal: " << commision << endl
    << "Average spent per person for the evening: " << avgSpent << endl;
    }


    int main()
    {
    ifstream infile;
    ofstream outfile;
    string outName = "c:\\datafiles\\result.dat";
    string inName = "c:\\datafiles\\orders.dat";

    int members, special, wine;
    float total;

    infile.open(inName.c_str());
    outfile.open(outName.c_str());
    outfile.precision(2);

    if (!infile)
    {
    cout << "Cannot open infile " << infile << " Aborting!" << endl;
    exit(1);
    }

    if (!outfile)
    {
    cout << "Cannot open oufile " << outfile << " Aborting!." << endl;
    exit(1);
    }


    checkSpecials(infile);

    checkSpecials(infile, outfile);

    infile.close();
    outfile.close();

    return 0;
    }[\CODE]

  2. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Reading data from files

    when you open a file , you got to specify the mode , as in std::ios::binary || std::ios::in as in reading and then std::ios:ut as in writing

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Reading data from files

    1) By default, ifstream has mode ios::read and ofstream has mode ios::out, so there
    is no need to explicitly set them (but it does not hurt to do so).

    2) The problem: checkSpecials() (1 argument version) reads th etire input file
    untill end of file.You then try to read the file again. This will fail. You need to
    do 2 things after reading the file the first time:

    a) reset the internal flags
    b) go back to the start of the file

    3) so at the end of checkSpecials, add the following:

    Code:
    infile.clear();
    infile.seekg(0,ios::beg);

  4. #4
    Join Date
    Feb 2011
    Posts
    2

    Talking Re: Reading data from files

    Thank you Philip that sorted it out.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured