MPNation
MPNation

Reputation: 179

How to read from a file in C++

I am trying to read in from a file named"parking.txt", and I want to read certain values from this file and output them to the screen. How can this be done? Can this be done?

The values in parking.txt is:

total 5
One 400
Five 300
Ten 200
Twenty 50
Quarter 500

In my code I would like to replace "line" with the appropriate value from the file.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inputFile ("parking_account.txt");
    string line;

    getline(inputFile, line);

    cout <<"\n\t-------------------------------------------------------";
    cout <<"\n\t=======================================================";
    cout <<"\n\t              Parking Machine Accounts                 ";
    cout <<"\n\t=======================================================";
    cout <<"\n\tSr. No.  : Bill Name       :  Bill Count  :  Cost(in$) ";
    cout <<"\n\t-------------------------------------------------------";
    cout <<"\n\t       1 : One Dollar      :  " << line << "  :  ";
    cout <<"\n\t       2 : Five Dollar     :  " << line << "  :  ";
    cout <<"\n\t       3 : Ten Dollar      :  " << line << "  :  ";
    cout <<"\n\t       4 : Twenty Dollar   :  " << line << "  :  ";
    cout <<"\n\t       5 : Quarter         :  " << line << "  :  ";

    cout<<"\n\tTotal bill types found : " <<line <<endl;
}

I have tried a while loop that searches line by line, but it outputs 5 of the same menus with line updated for that text value. Here is the while loop.

int main()
{
    ifstream inputFile ("parking_account.txt");
    string line;

    getline(inputFile, line);
    while (inputFile)
    {
        cout <<"\n\t-------------------------------------------------------";
        cout <<"\n\t=======================================================";
        cout <<"\n\t              Parking Machine Accounts                 ";
        cout <<"\n\t=======================================================";
        cout <<"\n\tSr. No.  : Bill Name       :  Bill Count  :  Cost(in$) ";
        cout <<"\n\t-------------------------------------------------------";
        cout <<"\n\t       1 : One Dollar      :  " << line << "  :  ";
        cout <<"\n\t       2 : Five Dollar     :  " << line << "  :  ";
        cout <<"\n\t       3 : Ten Dollar      :  " << line << "  :  ";
        cout <<"\n\t       4 : Twenty Dollar   :  " << line << "  :  ";
        cout <<"\n\t       5 : Quarter         :  " << line << "  :  ";

        cout<<"\n\tTotal bill types found : " <<line <<endl;
        getline(inputFile, line);
    }
}

Upvotes: 1

Views: 280

Answers (2)

Andreas DM
Andreas DM

Reputation: 11018

You should check if the file can be opened. If you can open the file, read the values from the file into your variables. You could do something like this:

If this is parking_account.txt:

5 400 300 200 50 500

And this is main.cpp:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
  ifstream inputFile("parking_account.txt");
  string line = "";
  int total = 0;
  int one = 0;
  int five = 0;
  int ten = 0;
  int twenty = 0;
  int quarter = 0;

  if (!inputFile.is_open()) {
    cerr << "Could not read from file" << endl;
  }
  else {
    inputFile >> total >> one >> five
    >> ten >> twenty >> quarter;
  }
  getline(inputFile, line);

  cout <<"\n\t-------------------------------------------------------";
  cout <<"\n\t=======================================================";
  cout <<"\n\t              Parking Machine Accounts                 ";
  cout <<"\n\t=======================================================";
  cout <<"\n\tSr. No.  : Bill Name       :  Bill Count  :  Cost(in$) ";
  cout <<"\n\t-------------------------------------------------------";
  cout <<"\n\t       1 : One Dollar      :  " << one << "  :  ";
  cout <<"\n\t       2 : Five Dollar     :  " << five << "  :  ";
  cout <<"\n\t       3 : Ten Dollar      :  " << ten << "  :  ";
  cout <<"\n\t       4 : Twenty Dollar   :  " << twenty << "  :  ";
  cout <<"\n\t       5 : Quarter         :  " << quarter << "  :  ";

  cout<<"\n\tTotal bill types found : " << total <<endl;

  return 0;
}

Upvotes: 0

Los Frijoles
Los Frijoles

Reputation: 4821

Try using the extraction operator >>:

string dummy; //this holds those separators since I have assumed that the numbers are always in the same order
//alternately, you could extract this two `>>`'s at a time, processing the string that
//comes befor the number to determine where it should go. For simplicity, I have
//assumed that the order is always the same.

int total one, five, ten, twenty, quarter;
inputFile >> dummy >> total >> dummy >> one >> dummy >> five >> dummy >> ten >> dummy >> twenty >> dummy >> quarter;

What this does is first extract your "Total" string into dummy. Next, it extracts the value "5" into the integer total. After that, it extracts "One" into dummy, 400 into one as an integer, "Two" into dummy, "300" into five as an integer, and so forth. If I've mis-interpreted your string format it should be simple enough to modify the above to match.

You can then replace your line variable in your output with the appropriate variable holding the value you are interested in for your table (one, five, etc).

The >> operator is provided by istream and is useful for these sorts of scenarios. (it's useful to note that this works on cin as well since cin's class is descended from istream, just as ifstream is descended from istream)

Upvotes: 1

Related Questions