从文件读取 (C++)
我不明白为什么这不会从我的文件中读取...
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int acctNum;
int checks;
double interest;
double acctBal;
double monthlyFee;
const int COL_SZ = 3;
ifstream fileIn;
fileIn.open("BankAccounts.txt");
if(fileIn.fail())
{
cout << "File couldn't open." << endl;
}
else
{
cout << left;
cout << "Bank Account records:" << endl;
cout << setw(COL_SZ) << "Account#" << setw(COL_SZ) <<
"Balance" << setw(COL_SZ) << "Interest" << setw(COL_SZ) << "Monthly Fee" << setw(COL_SZ) <<
"Allowed Checks" << setw(COL_SZ) << endl;
while(fileIn >> acctNum >> acctBal >> interest >> monthlyFee >> checks)
{
cout << setw(COL_SZ) << acctNum << setw(COL_SZ) << acctBal << setw(COL_SZ) << interest << setw(COL_SZ) <<
monthlyFee << setw(COL_SZ) << checks << endl;
}
}
fileIn.close();
system("pause");
return 0;
}
我拿出 ios::out 并放入 ios::in 同样的事情发生了没有数据和同样的事情把 ios 全部拿出来在一起。我确实从以前的程序中创建了该文件...我是否必须将文件代码的读取放入该程序中?
I can't figure out why this won't read from my file...
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int acctNum;
int checks;
double interest;
double acctBal;
double monthlyFee;
const int COL_SZ = 3;
ifstream fileIn;
fileIn.open("BankAccounts.txt");
if(fileIn.fail())
{
cout << "File couldn't open." << endl;
}
else
{
cout << left;
cout << "Bank Account records:" << endl;
cout << setw(COL_SZ) << "Account#" << setw(COL_SZ) <<
"Balance" << setw(COL_SZ) << "Interest" << setw(COL_SZ) << "Monthly Fee" << setw(COL_SZ) <<
"Allowed Checks" << setw(COL_SZ) << endl;
while(fileIn >> acctNum >> acctBal >> interest >> monthlyFee >> checks)
{
cout << setw(COL_SZ) << acctNum << setw(COL_SZ) << acctBal << setw(COL_SZ) << interest << setw(COL_SZ) <<
monthlyFee << setw(COL_SZ) << checks << endl;
}
}
fileIn.close();
system("pause");
return 0;
}
I took out the ios::out and put in ios::in same thing happened no data and the same thing with taking ios out all together. I did make the file from a previous program...would i have to put the reading of the files code into that program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
查看您的输入,您无法仅使用此代码读取如此复杂的输入,
该代码设置为读取以下形式格式化的数据:
相反,您必须在刮出之前读取各种字符串等您需要的数据。例如,要跳过下面的“Account”一词,您可以将其读入虚拟字符串
然后要获取数字,您必须读取下一个字符串并提取 #1234
但您也可以使用 getline 读取并包括 #
然后读取 # 之后的数字
所以如果您输入的是真正按照您所描述的方式格式化,您将不得不花费比您预期更多的时间来弄清楚如何解析数据。我不太了解您的输入如何为您提供完整的答案,但以上内容应该可以帮助您开始。
(您可以选择学习正则表达式,但此时您可能只想学习基础知识。)
原始
我刚刚尝试了您的代码,并且在输入中有足够的格式良好的值,它在 g++ 中工作。然而,我在查看您的代码时要小心的一件事是这一行:
如果由于文件过早结束而导致上述任何内容无法读取,您的 cout 将不会被执行,从而导致屏幕上没有输出。您的输入是否具有上述所有值?它们的格式正确吗?为了调试,我可能会尝试分解读取:
或者只是使用调试器单步执行。
例如对于此输入:
你的代码打印出来
但是用输入搞砸并在某处添加随机非数字字符,即:
让我得到只是
所以我肯定会逐件查看正在读取的内容以及从
fileIn
读取失败的地方,这肯定会导致您的问题。您当然知道要按照指定的 删除
ios::out
这里Edit
Looking at your input you can't read such complex input with just
This code is setup to read data formatted in the following form:
Instead you'll have to read the various strings and such before scraping out the data you need. For example to skip over the word "Account" below, you can read it into a dummy string
Then To get the number you'll have to read the next string and extract the #1234
But you could also use getline to read up to and including the #
Then read in the number after the #
So if you're input is truly formatted as you describe, you'll have to spend a lot more time figuring out how to parse your data then you may have expected. I don't know enough about how your input is expected to give you a complete answer, but the above should hopefully get you started.
(Optionally, you could learn about regular expressions, but at this point you may just want to learn the basics.)
Original
I just tried your code out and with enough well-formatted values in the input, it works in g++. However, one thing that I am wary of looking at your code is this line:
If any of the above fails to read due to the file ending prematurely, your cout isn't going to get executed, causing no output to the screen. Does your input have all the above values? Are they well formatted? To debug I might try breaking up the reads:
or just step through with a debugger.
For example for this input:
your code prints out
But screwing with the input and adding a random non numeric character somewhere, ie:
causes me to get just
So I would definitely look piece-by-piece at what's getting read and where a read from
fileIn
is failing, this would definitely cause your problems.You know of course to remove the
ios::out
as specified here您
正在打开文件进行输出。尝试 ios::in。
You have
You're opening the file for output. Try ios::in.