c++ - 关于文件I/O的疑问

标签 c++ file-io

目前我刚刚开始使用 C++,想深入研究文件 I/O,所以我搜索了一些随机代码并键入它以查看它是否有效以及它是如何工作的。但是我遇到了一些自己无法理解的问题。

#include <fstream>  //for file processing
#include <iostream>
#include <vector>
#include <string> //to_string

using namespace std;

int main ( int argc, char *argv[] )
{
    if ( argc != 3 ) 
{
    cerr << "Incorrect number of arguments" << endl;
    return 1;
}
//open file at argv[1] ( should be our input file )
ifstream inputFile ( argv[1] );
ofstream outputFile ( argv[2] );

// check if file opening succeeded
if ( !inputFile.is_open() )
{
    cerr << "Could not open the input file\n";
    return 1;
}
else if( !outputFile.is_open() )
{
    cerr << "Could not open the output file\n";
    return 1;
}
//declare a vector of integers
vector<int> numbers;

int numberOfEntries;

//get the first value in the inputFile that has the number of elements in the file
inputFile >> numberOfEntries;

//iterate through the inputFile until there are no more numbers
for( int i = 0; i < numberOfEntries; ++i )
{
    //get next number from inputFile
    int number;
    inputFile >> number;

    //store number in the vector
    numbers.push_back( number );
}

//iterate through the vector (need c++11)
for( int n : numbers )
{
    //write to the output file with each number multiplied by 5
    outputFile << (n*5);

    //add a line to the end so the file is readable
    outputFile << "\n";
}
return 0;
}

所以我有了这段代码并编译了它。它只会显示不正确的参数数量。出了什么问题?

最佳答案

Spidey 是正确的,但是,重要的是,当您开始像这样编程时,通读代码并将其分解为您可以理解的部分。

#include <fstream>  //for file processing
#include <iostream>
#include <vector>
#include <string> //to_string
using namespace std;

您应该将这些识别为 include 指令 - 使用与 I/O 相关的库,就像您预期的那样。

int main ( int argc, char *argv[] )
{
    if ( argc != 3 ) 
{
    cerr << "Incorrect number of arguments" << endl;
    return 1;
}

这是抛出错误的地方。 argc 正在检查向应用程序提供了多少参数 - 如果数字不是 3,程序将返回一条消息,并以 的结果退出>1 - 一个成功的程序总是通过比较返回 0

我们可以通过分析接下来的几行来复核这个假设:

//open file at argv[1] ( should be our input file )
ifstream inputFile ( argv[1] );
ofstream outputFile ( argv[2] );

看到了吗?它正在检查在参数位置 [1][2] 处提供的文件 - 我们的初始假设是正确的。该程序需要在命令行提供多个文件;没有它们就无法运行。因此,当程序意识到它没有正确数量的文件时,它会提前退出。

// check if file opening succeeded
if ( !inputFile.is_open() )
{
    cerr << "Could not open the input file\n";
    return 1;
}
else if( !outputFile.is_open() )
{
    cerr << "Could not open the output file\n";
    return 1;
}

这些行将尝试打开文件,如果无法打开(例如,如果它们不存在),则返回错误消息并提前退出。

//declare a vector of integers
vector<int> numbers;

int numberOfEntries;

//get the first value in the inputFile that has the number of elements in the file
inputFile >> numberOfEntries;

//iterate through the inputFile until there are no more numbers
for( int i = 0; i < numberOfEntries; ++i )
{
    //get next number from inputFile
    int number;
    inputFile >> number;

    //store number in the vector
    numbers.push_back( number );
}

//iterate through the vector (need c++11)
for( int n : numbers )
{
    //write to the output file with each number multiplied by 5
    outputFile << (n*5);

    //add a line to the end so the file is readable
    outputFile << "\n";
}

此代码循环遍历 inputFile,查找数字,并将它们输出到 outputFile

现在我们知道整个程序是从一个文件读取数字并写入另一个文件的练习。这是一个简单的 I/O 示例。

return 0;

还记得我说过一个成功的程序返回 0 吗?好吧,在所有代码运行之后,程序就是这样做的。

编辑:要直接回答您的问题,该程序需要提供两个文件作为文件名。这样的例子是 g++ -o fileExample fileExample.cpp input.txt output.txt 其中 input.txt 文件包含数字行,而 output.txt 已创建,位于与 input.txt 和 .fileExample.cpp 相同的位置。

关于c++ - 关于文件I/O的疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32344270/

相关文章:

使用 Apple 的 LLVM 编译器编译 -O 时出现 C++ 代码段错误,但使用 g++ -7.2.0 时则不会

c++ - 无法跨 DLL 传递 std::wstring

c++ - 从文件中读取分隔符

c - 如何从文件中读取数字并将其分配给它们的实际含义

c - WriteFile 函数 - 写入串口

JAVA从String转换为Binary,然后返回文件中的String

javascript - 是否可以使用 javascript/meteor 附加并保存 css 文件?

java - 有没有办法从网络应用程序读取目录的内容

c++ - nullptr 右值如何

c++ - 按大于 vector 大小的值旋转 vector 元素?