c++ - 即使将数据保存在文件中,该文件处理代码也不会从文件中读取,这是怎么回事?
问题描述
我正在做一个大学项目,这是我开发的铁路票务系统。现在,问题是数据正在保存在创建的文件中,但是当我想从文件中读取该数据时,控制台窗口中没有任何显示。我尝试过多次更改它,但问题仍然存在。我似乎无法找到解决方案,如果你们能提供帮助,那就太好了。
else if (choice == 4)
{
char b[13];
cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
cout << "\nPress 2 to display Lahore to Karachi Queue";
cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
cin >> fchoice;
if (fchoice == 1)
{
char b[] = "rwptolhr.txt";
}
else if (fchoice == 2)
{
char b[] = "lhrtokch.txt";
}
else if (fchoice == 3)
{
char b[] = "rwptokch.txt";
}
fstream file;
file.open(b, ios::in | ios::app);
if (!file)
{
cout << "\n\nError in opening file!!!" << endl;
}
cout << "\n\nFile content: " << endl;
//reading and extracting data from file.
for (string line; getline(file, line);)
{
while (string::npos)
{
int count = 0; //to count the number of name characters before space
for (int i = 0; line[i] != ' '; i++)
{
count++;
}
string customername = line.substr(0, count);
cout << "\n\nName: " << customername << endl;
int fcount = 0; //to count the number of age characters before space
for (int i = count + 1; line[i] != ' '; i++)
{
fcount++;
} //please note that "+1 +2 etc in the counter //variables" are given because there is spaces in the string.
string customerfrom = line.substr(count + 1, fcount);
cout << "From: " << customerfrom << endl;
int tcount = 0; //to count the program characters
for (int i = count + fcount + 2; line[i] != ' '; i++)
{
tcount++;
}
string customerto = line.substr(fcount + count + 2, tcount);
cout << "To: " << customerto << endl;
int pcount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
{
pcount++;
}
string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
cout << "Payment: " << customerpayment << endl;
int ocount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}
解决方案
更改char b[13]
为string b
对我有用。
else if (choice == 4)
{
string b;
cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
cout << "\nPress 2 to display Lahore to Karachi Queue";
cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
cin >> fchoice;
if (fchoice == 1)
{
b = "rwptolhr.txt";
}
else if (fchoice == 2)
{
b = "lhrtokch.txt";
}
else if (fchoice == 3)
{
b = "rwptokch.txt";
}
fstream file;
file.open(b, ios::in | ios::app);
if (!file)
{
cout << "\n\nError in opening file!!!" << endl;
}
cout << "\n\nFile content: " << endl;
//reading and extracting data from file.
for (string line; getline(file, line);)
{
while (string::npos)
{
int count = 0; //to count the number of name characters before space
for (int i = 0; line[i] != ' '; i++)
{
count++;
}
string customername = line.substr(0, count);
cout << "\n\nName: " << customername << endl;
int fcount = 0; //to count the number of age characters before space
for (int i = count + 1; line[i] != ' '; i++)
{
fcount++;
} //please note that "+1 +2 etc in the counter //variables" are given because there is spaces in the string.
string customerfrom = line.substr(count + 1, fcount);
cout << "From: " << customerfrom << endl;
int tcount = 0; //to count the program characters
for (int i = count + fcount + 2; line[i] != ' '; i++)
{
tcount++;
}
string customerto = line.substr(fcount + count + 2, tcount);
cout << "To: " << customerto << endl;
int pcount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
{
pcount++;
}
string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
cout << "Payment: " << customerpayment << endl;
int ocount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}
推荐阅读
- powershell - -PropertyType 设置为 DWord,但在创建项目时它以字符串形式出现
- xml - 使用xslt将元素从父节点重复到子节点
- android - 创建有价值的资源文件
- flutter - 如何用底部创建 SliverAppBar
- python - PySpark 上 GBT 分类器的交叉验证在 2 GB 数据上花费了太多时间(80% 训练和 20% 测试)。有没有办法减少时间?
- php - 为什么我应该在 php 中使用 reset($arr) 而不是 $arr[0] 来生成 php 中数组的第一个元素?
- kubernetes - Kubernetes 仪表板错误消息:禁止配置映射:用户“system:serviceaccount:kube-system:deployment-controller”无法列出资源
- postgresql - Postgresql 12 CAST now() 迄今为止无法正常工作
- c++ - 如何在 dlib 中重复矩阵?
- react-native - ScrollView 完全占用父空间