gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:47 26 4
gpt4 key购买 nike

我正在尝试从名为“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com