C++ 根据ID/注册搜索和编辑.txt fstream文件
我试图从.txt中搜索特定的ID/注册,并相应地显示相应的信息。在这种情况下,我希望根据应输入的相应注册号显示定价 读和写文件不是我的问题。网上有很多关于阅读和写作的信息,但根据ID/注册进行搜索和显示的信息不多C++ 根据ID/注册搜索和编辑.txt fstream文件,c++,fstream,C++,Fstream,我试图从.txt中搜索特定的ID/注册,并相应地显示相应的信息。在这种情况下,我希望根据应输入的相应注册号显示定价 读和写文件不是我的问题。网上有很多关于阅读和写作的信息,但根据ID/注册进行搜索和显示的信息不多 #include <iostream> #include <iomanip> #include <fstream> #include <cstdlib> #include <string> using namespace s
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string line;
double cost;
string reg;
ifstream in_stream;
ofstream out_stream;
char registration[10];
//Open file
in_stream.open("Fines.dat");
//Error if opening fails
if (in_stream.fail())
{
cout << "Input file could not open. " << endl;
exit(1);
}
//Open out stream file
out_stream.open("OutStandingFines.dat");
//Error if opening fails
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
//Display original .dat file
cout <<"Original .dat File" << endl;
if(in_stream.is_open())
{
while(in_stream >> reg >> cost)
{
cout << reg <<" " << cost << '\n';
}
in_stream.close();
}
else
{
cout <<"File is not open: " << endl;
}
/////////////My problem is from here//////////////////////////
//Enter the registration number you wish to search
cout << "Please enter registration number: " << endl;
cin >>registration;
//I must display all cost values that have the same registration number???????? I need help with this
/*
for (int i = 0; i < 10; i++)
{
if( reg == registration)
{
cout << fixed << setw(2)<< setprecision(2) <<"R " << cost << '\n';
out_stream << fixed << setw(2)<< setprecision(2) << "R "<< cost << endl; //send back to .dat
}
}
*/
in_stream.close();
out_stream.close();
system("pause");
return(0);
}
好的,我设法得到了输出。现在的问题是显示具有相同注册/ID的所有值 由于某些原因,当我输入ABC123时,只显示fstream.txt中的最后一行 txt包含以下信息。
只需按照添加了if语句的代码中更高的方式读取文件?您可能还想考虑将数据加载到内存或使用某种类型的数据库,但进入这一点有点超出评论/答案的范围。
ABC123 400
DEC234 340
ABC123 500
GED345 600
ABC123 240
GEE600 120
GED345 230
GEE600 470
ABC123 120
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string line;
double cost;
string reg;
ifstream in_stream;
ofstream out_stream;
string registration;
in_stream.open("Fines.dat");
if (in_stream.fail())
{
cout << "Input file could not open. " << endl;
exit(1);
}
out_stream.open("OutStandingFines.dat");
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
//Display original .dat file
cout <<"Original .dat File" << endl;
if(in_stream.is_open())
{
while(in_stream >> reg >> cost)
{
cout << reg <<" " << cost << '\n';
}
}
else
{
cout <<"File is not open: " << endl;
}
//Enter Registration here
cout << "Please enter registration number: " << endl;
cin >>registration;
//compare and display all registration numbers that match
cout <<"Fines: " << endl;
if(out_stream.is_open())
{
while(reg == registration)
{
cout << fixed << setw(2)<< setprecision(2) <<"R " << cost << '\n';
out_stream << fixed << setw(2)<< setprecision(2) << "R "<< cost << endl; //send back to .dat
exit(1);
}
}
else
{
cout <<"File is not open: " << endl;
}
in_stream.close();
out_stream.close();
system("pause");
return(0);
}