【问题标题】:Using I/O to compare files, keep getting runtime error使用 I/O 比较文件,不断收到运行时错误
【发布时间】:2012-10-06 18:45:58
【问题描述】:

我有一个任务要求

a.) Prompts the user to input the names of 3 files: two input files
and an output file.  
b.) Reads in the two input files line by line and compares the two
lines.
c.) For each line in the inputs, the output should have the line
number, a colon (':'), and either "OK" if the lines match or "DIFF" if
the lines are different.
The input files may be of different lengths. 
- Your program should be case-sensitive, so you do NOT need to worry about converting 

text to lowercase or uppercase.

For example:

input1:
abc
def
g h i

input2:
abc
DEf
ghi
uub

output:
1:OK
2:DIFF
3:DIFF
4:DIFF

基本上我已经编写了代码,但是每次我尝试在 Putty 上运行它时,它都能正确编译。

:: a.out
Please enter input file name: abc
terminate called after throwing an instance of 'std::runtime_error'
  what():  Can't open input file: abc
Abort (core dumped)

所以在我输入我的文件名之前,它得到了返回错误,我根据 Stroustrup 的书写了我输入和输出的每个部分。我到底错过了什么,或者我只是做错了? 谢谢。

#include "std_lib_facilities_3.h"

int main()
{
    //Input 1
    cout  << "Please enter input file name: ";
    string name;
    cin >> name;
    ifstream ist1(name.c_str());
    if (!ist1) error("Can't open input file: ",name);

    //Input 2
    cout << "Please enter another input file name: ";
    string name2;
    cin >> name2;
    ifstream ist2(name2.c_str());
    if (!ist2) error("Can't open input file name: ",name2);

    //Output
    cout << "Please enter name of output file: ";
    string oname;
    cin >> oname;
    ofstream ost(oname.c_str());
    if (!ost) error("Can't open output file: ",oname);

    string s, t;
    int i = 1;
    int flag = 1;
    while(true) 
    {
        if (!getline(ist1, s)) {flag = 1; break;}
        if (!getline(ist2, t)) {flag = 2; break;}
        ost << i;
        if (s == t)
            ost << ": OK\n";
        else
            ost << ": DIFF\n";
        i++;
    }

    if (flag == 2) {
        ost << i << ": DIFF\n"; 
        i++;
        while (getline(ist1, s)) {ost << i << ": DIFF\n"; i++;}
    }

    if (flag == 1) {
        while (getline(ist2, t)) {ost << i << ": DIFF\n"; i++;}
    }

    return 0;
}

【问题讨论】:

  • 是的,但我不确定它应该是什么扩展......?如果那有意义的话。我有 abc.txt 但它什么也没做。我在同一根文件夹下看到名为“core”(文件)和“a.out”(OUT 文件)的其他文件,我应该相应地编辑它们吗?
  • 那你的问题;你告诉你的程序输入文件是abc,但它是abc.txt
  • lol dang,好吧,我尝试输入“abc.txt”,但仍然遇到相同的运行时错误。然后我继续复制“Core”并将其重命名为“abc”,以及其他三个文件,所以我的输入 1 是 abc,输入 2 是 yes,输出是 hola。现在腻子实际上正在做某事,我确定它卡在了 while 循环中。
  • 除非您知道自己在做什么,否则您可能不想使用core 作为输入文件; core 是一个二进制调试转储,几乎可以肯定不会与您的任何其他文本文件对齐。
  • 啊,好吧,明白了。核心实际上是在我的根文件夹本身上弹出的。难怪它从 3mb 开始就永远使用 Putty。由于我不断收到 abc.txt 相同的运行时错误,我应该使用哪种类型的文件扩展名?

标签: c++ input io compare


【解决方案1】:

您的程序没问题,但您需要确保输入包括文件扩展名在内的整个文件名。我能够使用这些文件成功运行您的程序:

  • program.cc — 你的程序的源代码
  • program — 你的程序,已编译
  • file-a.txt — 一个文本文件:

    Hello, world!
    This is a file.
    This line is different.
    But same again.
    
  • file-b.txt — 另一个文本文件:

    Hello, world!
    This is a file.
    This line differs from file A.
    But same again.
    

然后我能够成功运行您的程序:

$ ./program
Please enter input file name: file-a.txt
Please enter another input file name: file-b.txt
Please enter name of output file: diffed.txt

它成功地创建了diffed.txt,并带有这个输出:

1: OK
2: OK
3: DIFF
4: OK

【讨论】:

  • 是的!它起作用了 :D 我的问题是我的 abc.txt 文件实际上是.... abc.txt.txt 无法相信这是我的问题。我使用了您的示例,它确实创建了 diffed.txt ----但是它正在度过甜蜜的时光,这应该发生吗?我还没有得到 1.OK、2、OK、3.DIFF 和 4.OK。
  • ^作为回应,我 ctrl+z 它变成了一个 615mb 的文件。 (到底发生了什么)。
  • @enigmuz:不,它对我来说运行得非常快。我希望您仍然没有使用该核心文件作为输入之一;这可能会导致这样的问题。
  • 很奇怪,我用您提供的内容创建了两个 txt (file-a,file-b) 文件,然后运行了我的代码。它确实创建了 diffed.txt,但是 Putty 仍然继续运行并使 diffed.txt 文件越来越大。我确保删除了根文件夹中的所有内容,并且只有我的程序和两个 txt 文件。也许是我的编译器或腻子导致了这种情况?不完全确定。好消息是,至少代码有效。
  • 解决了我的问题,这只是一些愚蠢的技术问题,现在一切正常 :D 谢谢 icktoofay! :D
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
相关资源
最近更新 更多