c++ - C++中如何读取文件

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

最佳答案

尝试使用提取运算符 >> :

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;

这是首先将“Total”字符串提取到 dummy 中.接下来,它将值“5”提取到整数 total 中。 .之后,它将“一”提取到dummy中。 , 400 入 one作为一个整数,“二”变成dummy , "300"变成five作为一个整数,等等。如果我误解了您的字符串格式,修改上面的内容应该很简单。

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

>>运算符由 istream 提供并且对这些场景很有用。 (值得注意的是,这对 cin 也有效,因为 cin 的类是从 istream 派生的,就像 ifstream 是从 istream 派生的一样)

关于c++ - C++中如何读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749405/

相关文章:

c++ - 什么导致内存保留

c++ - volatile 成员变量

c++ - 在 Qt 中清除标志/提示

c++ - 了解旧 C++ 代码的行为

c++ - ld 找不到可用的库

c++ - DFS 在 kefa 和 park 中提供 TLE(codeforces 560C)

c++ - BigInt 转换(gmp Bigint 到 botan bigint)

c++ - 各种集合的模板类偏特化

c++ - 从 Visual Studio 解决方案生成 Make 文件(用于 GCC)

c++ - std::list 指针和 remove_if