简单的菜鸟 I/O 问题 (C++)
抱歉这个菜鸟问题,但我是 C++ 新手。
我需要从文件中逐行读取一些信息,并执行一些计算,然后输出到另一个文件中。例如,我们读取每行的唯一 ID、名称和 2 个数字。最后 2 个数字相乘,并在输出文件中逐行打印 ID、名称和产品:
input.txt:
2431 John Doe 2000 5
9856 Jane Doe 1800 2
4029 Jack Siu 3000 10
output.txt:
ID Name Total
2431 John Doe 10000
9856 Jane Doe 3600
4029 Jack Siu 30000
我的代码是与此类似,但输出文件中仅出现第一行。如果我重复按 Enter
,其他行会出现在输出文件中:
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
int main () {
int ID, I, J;
string First, Last;
char c;
cout << "ID\tName\t\Total\n";
while ((c = getchar()) != EOF) {
cin >> ID >> First >> Last >> I >> J;
cout << ID << " " << First << " " << Last << " " I * J << "\n";
}
return 0;
}
这是我唯一的问题,除非我按 Enter
,否则这些值不会出现在输出文件中重复,然后关闭程序。任何人都可以建议修复我上面的代码,让它在没有键盘输入的情况下完成任务吗?谢谢!
sorry for the noob question, but I'm new to C++.
I need to read some information, line-by-line, from a file, and perform some calculations, and output into another file. For example, we read a unique ID for each line, a name, and 2 numbers. The last 2 numbers are multiplied, and in the output file, the ID, name and product are printed line by line:
input.txt:
2431 John Doe 2000 5
9856 Jane Doe 1800 2
4029 Jack Siu 3000 10
output.txt:
ID Name Total
2431 John Doe 10000
9856 Jane Doe 3600
4029 Jack Siu 30000
My code is similar to this, but only the first line appears in the output file. If I press Enter
repeatedly, the other lines appear in the output file:
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
int main () {
int ID, I, J;
string First, Last;
char c;
cout << "ID\tName\t\Total\n";
while ((c = getchar()) != EOF) {
cin >> ID >> First >> Last >> I >> J;
cout << ID << " " << First << " " << Last << " " I * J << "\n";
}
return 0;
}
That's my only problem, that the values don't appear in the output file, unless I press Enter
repeatedly, then close the program. Can anyone suggest a fix for my code above, to have it do the task without keyboard input? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
Use
您已经隐藏了真正的 std::cin 和 std::cout...稍后将从它们中读取。
但这里你使用真正的 std::cin 来检查 EOF。
You've hidden the real std::cin and std::cout...and will later read from them.
But here you use the real std::cin to check for EOF.
getchar()
调用读取等待您键入字符(并按 Enter),因为它从 stdin(标准输入)读取。尝试更改循环条件以在cin
到达文件末尾时停止读取。编辑
您还应该为输入和输出流使用不同的名称 -
std
命名空间中已经有cin
和cout
。The
getchar()
call reads waits for you to type a character (and press Enter) since it reads from stdin (standard input). Try changing the loop condition to stop reading whencin
reaches end of file.EDIT
You should also use different names for input and output streams -- there are already
cin
andcout
in thestd
namespace.这是因为您在 while 循环条件中使用了 getchar() 。不确定您要做什么,但 getchar() 从标准输入读取一个字符。您应该做的是检查 cin 是否失败或遇到 EOF。
This is because you used getchar() in your while loop condition. Not sure what you were trying to do, but getchar() reads a char from stdin. What you should have done, is check if cin failed or encountered EOF.
当我寻找答案时,我最好检查一下并确保它有效。我遇到了一些构建错误,并且有点忘乎所以了。
希望这有帮助!
While I was looking for the answer I though I better check and make sure it worked. I got some build errors and got a little carried away from there.
Hope this helps!