如何从C ++中读取文件

时间:2014-11-05 03:50:45

标签: c++

我正在尝试从名为“parking.txt”的文件读入,我想从此文件中读取某些值并将其输出到屏幕。如何才能做到这一点?可以这样做吗?

parking.txt中的值为:

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

在我的代码中,我想用文件中的适当值替换“line”。

#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;
}

我尝试了一个逐行搜索的while循环,但它输出了5个相同的菜单,并为该文本值更新了行。这是while循环。

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);
    }
}

2 个答案:

答案 0 :(得分:1)

尝试使用提取运算符>>

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;

这样做首先提取你的&#34; Total&#34;字符串到dummy。接下来,它提取值&#34; 5&#34;整数total。之后,它提取出一个&#34; One&#34;进入dummy,进入one作为整数400,&#34; 2&#34;进入dummy,&#34; 300&#34;作为整数进入five,依此类推。如果我错误地解释了您的字符串格式,那么修改上面的内容就足够简单了。

然后,您可以使用适当的变量替换输出中的line变量,该变量包含您对表格感兴趣的值(onefive等)。

>>运算符由istream提供,对这些场景非常有用。 (请注意,此问题适用于cin,因为cin的类来自istream,正如ifstream下降一样来自istream

答案 1 :(得分:0)

您应该检查文件是否可以打开。 如果可以打开文件,请将文件中的值读入变量中。 你可以这样做:

如果是parking_account.txt

5 400 300 200 50 500

这是 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;
}