【问题标题】:Opening multiple files in C++在 C++ 中打开多个文件
【发布时间】:2010-03-05 16:09:50
【问题描述】:

我有这段代码可以在命令行中一次打开多个文件,然后如果它无法打开其中一个文件,它会关闭所有文件并退出。

/* Opens an array of files and returns a pointer to the first 
 * element (the first file).
 */
ifstream *OpenFiles(char * const fileNames[], size_t count)
{
    /* If no command line arguments, error and exit */
    if (count == 0) {
        cerr << "Invalid number of arguments.";
        exit(EXIT_FAILURE);
    }
    ifstream *fileObj;
    fileObj = new ifstream[count];

    if (fileObj == NULL) {
        cerr << "Failed to create space for files";
        exit(EXIT_FAILURE);
    }

    /* Opens one file at a time and closes all files if there is an 
     * error opening any file.
     */
    for (int loopCount = 0; loopCount < (int)count; loopCount++) { 
        fileObj[loopCount].open(fileNames[loopCount], ios::out); 
        if (!fileObj[loopCount].is_open()) { 
            cerr << "Failed to open " << fileNames[loopCount] << "\n";
            for (; loopCount >= 0; loopCount--) {
                fileObj[loopCount].close();
                cout << "Closed " << fileNames[loopCount] << "\n";
            }
            delete[] fileObj;
        } 
    }
    return fileObj;
}

我这样做是为了做家庭作业,我的老师有另一个检查员我们必须提交给我,并给了我这些类型的警告:

Assign8_1.cpp(44): error 445: (Warning -- Reuse of for loop variable 'loopCount' at 'line 40' could cause chaos)
    return fileObj;
Assign8_1.cpp(51): error 850: (Info -- for loop index variable 'loopCount' whose type category is 'integral' is modified in body of the for loop that began at 'line 40')
    return fileObj;
Assign8_1.cpp(51): error 449: (Warning -- Pointer variable 'fileObj' previously deallocated [Reference: file Assign8_1.cpp: lines 30, 48])
Assign8_1.cpp(30): error 831: (Info -- Reference cited in prior message)
Assign8_1.cpp(48): error 831: (Info -- Reference cited in prior message)
}
Assign8_1.cpp(63): error 818: (Info -- Pointer parameter 'files' (line 55) could be declared as pointing to const)
}

从第一个警告开始,我想知道为什么我不应该像在代码中那样使用我的 loopCount 变量两次。这就是我认为它会工作的方式,跟踪我正在查看的文件,适当地打开和关闭它。

有人知道错误 449 是什么意思吗?谢谢。

【问题讨论】:

  • 出于好奇,此输出似乎来自 Gimpel lint 工具之一。

标签: c++


【解决方案1】:

您需要在循环中delete[] fileObj 之后exit(EXIT_FAILURE),否则您将在下一次迭代中崩溃。这可能是警告 449 告诉您的内容。

除此之外,代码看起来还不错。但是,如果您希望它在没有这些警告的情况下编译,您可以将内部循环转换为仅使用 loopCount 作为边界的标准 for 循环。比如:

for (int i = loopCount; i >= 0; i--) {
    fileObj[i].close();
    cout << "Closed " << fileNames[i] << "\n";
}

【讨论】:

  • 最好将 i=0 改为 i
  • 感谢@Michael 的提示。我是先学 Java,所以经常忘记考虑无符号数据类型。
【解决方案2】:

你绝对可以这样使用你的 loopCount 变量,但它会给你带来一个错误。粗略一看,它看起来会永远运行,因为你永远不会从内循环中跳出外循环。您在内循环中递减 loopCount,并在外循环中来回递增。

虽然在内部循环中使用 loopCounter 并不违反语言,但它通常表明正在发生的事情并不是您真正想要的。

你最好使用这样的代码:

list<ifstream> files; 

ifstream file("name"); 
if ( file.is_open() ) { 
   files.push_back(file); 
}  

// close files 
for ( list<ifstream>::iterator i = files.begin(); iter++; i != files.end() ) { 
  i->close();   
}

【讨论】:

    【解决方案3】:

    我将代码结构更像这样:

    for all input names
        if (!open file)
            break;
    
    if (number of open files != number of input names) 
        close the files that did open
    return (possibly empty) list of files
    

    【讨论】:

      【解决方案4】:

      449:

      删除数组后,您将开始尝试再次打开文件 - 如果任何打开调用失败,您将永远陷入循环。 删除数组后,您需要 returnbreak

      【讨论】:

        猜你喜欢
        • 2010-11-21
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        相关资源
        最近更新 更多