即使将数据保存在文件中,此文件处理代码如何不从文件中读取?
How is it that this file handling code doesn't read from files even though it saves data in the files?
我正在做一个大学项目,这是我开发的铁路票务系统。现在,问题是数据被保存在创建的文件中,但是当我想从文件中读取数据时,控制台中没有任何显示 window。我已经尝试多次更改它,但问题仍然存在。我似乎找不到解决这个问题的方法,如果你们能提供帮助就太好了。
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] != '[=10=]'; 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] != '[=10=]'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}
我正在做一个大学项目,这是我开发的铁路票务系统。现在,问题是数据被保存在创建的文件中,但是当我想从文件中读取数据时,控制台中没有任何显示 window。我已经尝试多次更改它,但问题仍然存在。我似乎找不到解决这个问题的方法,如果你们能提供帮助就太好了。
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] != '[=10=]'; 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] != '[=10=]'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}