C++ 如何在C++;
我试图从一个名为“parking.txt”的文件中读取数据,我想从这个文件中读取某些值并将它们输出到屏幕上。如何做到这一点?这能做到吗 parking.txt中的值为:C++ 如何在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
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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入文件(“parking_account.txt”);
弦线;
getline(输入文件,行);
请尝试使用提取运算符>
:
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
。然后,将“One”提取到dummy
,将400提取到One
作为整数,将“Two”提取到dummy
,“300”将five
转换为整数,以此类推。如果我错误地解释了您的字符串格式,那么应该很简单,可以修改以上内容以匹配
然后,您可以将输出中的行
变量替换为包含您感兴趣的表值的适当变量(one
,five
,等等)
>
操作符是由istream
提供的,对于这类场景很有用。(需要注意的是,这也适用于cin
,因为cin
的类是从istream
派生的,就像ifstream
是从istream
派生的一样。)您应该检查文件是否可以打开。
如果可以打开文件,请将文件中的值读取到变量中。
你可以这样做:
如果这是parking\u account.txt
:
5 400 300 200 50 500
这是main.cpp:
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入文件(“parking_account.txt”);
字符串行=”;
int-total=0;
int-one=0;
int五=0;
int-ten=0;
int二十=0;
四分之一整数=0;
如果(!inputFile.is_open()){
cerr总计>>一>>五
>>十个>>二十个>>季度;
}
getline(输入文件,行);
问题和代码段引用了不同的输入文件,解决了这个问题,并从循环中删除了示例输出,这是为了演示,而不是为了重用。
5 400 300 200 50 500
#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;
}