简单的菜鸟 I/O 问题 (C++)

发布于 2024-09-08 21:11:39 字数 1071 浏览 7 评论 0原文

抱歉这个菜鸟问题,但我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

喵星人汪星人 2024-09-15 21:11:39

使用

while (!cin.eof()) {

Use

while (!cin.eof()) {
说谎友 2024-09-15 21:11:39
using namespace std;

ifstream cin("input.txt");
ofstream cout("output.txt");

您已经隐藏了真正的 std::cin 和 std::cout...稍后将从它们中读取。

while ((c = getchar()) != EOF) {

但这里你使用真正的 std::cin 来检查 EOF。

using namespace std;

ifstream cin("input.txt");
ofstream cout("output.txt");

You've hidden the real std::cin and std::cout...and will later read from them.

while ((c = getchar()) != EOF) {

But here you use the real std::cin to check for EOF.

眼泪也成诗 2024-09-15 21:11:39

getchar() 调用读取等待您键入字符(并按 Enter),因为它从 stdin(标准输入)读取。尝试更改循环条件以在 cin 到达文件末尾时停止读取。

编辑
您还应该为输入和输出流使用不同的名称 - std 命名空间中已经有 cincout

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 when cin reaches end of file.

EDIT
You should also use different names for input and output streams -- there are already cin and cout in the std namespace.

ゞ花落谁相伴 2024-09-15 21:11:39

这是因为您在 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.

想挽留 2024-09-15 21:11:39

当我寻找答案时,我最好检查一下并确保它有效。我遇到了一些构建错误,并且有点忘乎所以了。

希望这有帮助!

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    ifstream indata("input.txt");
    if(!indata)
    { // file couldn't be opened
        cerr << "Error: input.txt could not be opened" << endl;
        exit(1);
    }

    ofstream output("output.txt");
    if(!output)
    { // file couldn't be opened
        cerr << "Error: output.txt could not be opened" << endl;
        exit(1);
    }

    int ID, I, J;
    char First[10], Last[10];

    output << "ID\tName\tTotal\n";
    while (!indata.eof()) 
    {
        indata >> ID >> First >> Last >> I >> J;
        output << ID << " " << First << " " << Last << " " << I * J << endl;
    }

    indata.close();
    output.close();

    return 0;
}

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!

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    ifstream indata("input.txt");
    if(!indata)
    { // file couldn't be opened
        cerr << "Error: input.txt could not be opened" << endl;
        exit(1);
    }

    ofstream output("output.txt");
    if(!output)
    { // file couldn't be opened
        cerr << "Error: output.txt could not be opened" << endl;
        exit(1);
    }

    int ID, I, J;
    char First[10], Last[10];

    output << "ID\tName\tTotal\n";
    while (!indata.eof()) 
    {
        indata >> ID >> First >> Last >> I >> J;
        output << ID << " " << First << " " << Last << " " << I * J << endl;
    }

    indata.close();
    output.close();

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文