简体   繁体   English

怀疑文件I / O.

[英]Doubts about the file I/O

Currently I'm just getting started on c++ and wanted to take a dive into file I/O so I searched for some random code and typed it to see if it works and how it works. 目前我刚刚开始使用c ++并希望深入研究文件I / O,因此我搜索了一些随机代码并输入它以查看它是否有效以及它是如何工作的。 But I ran into some problems that myself cannot understand. 但是我遇到了一些我自己无法理解的问题。

#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;
}

So I had this code and I compiled it. 所以我有这个代码,我编译了它。 It would only show me Incorrect number of arguments . 它只会显示Incorrect number of arguments What is going wrong? 出了什么问题?

Spidey is correct, however, it's important that when you begin programming like this, that you read through the code and break it down into parts you can understand. Spidey是正确的,但重要的是,当你开始这样的编程时,你会通读代码并将其分解为你能理解的部分。

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

You should recognize these as include directives - using libraries related to I/O, like you expected. 你应该将它们识别为包含指令 - 使用与I / O相关的库,就像你期望的那样。

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

This is where the error is being thrown. 这是抛出错误的地方。 argc is checking how many arguments were provided to the application - if the number is anything other than 3 , the program will return a message, and exit with the result of 1 - a successful program always returns 0 by comparison. argc正在检查向应用程序提供了多少个参数 - 如果数字不是3 ,程序将返回一条消息,并以1结果退出 - 成功的程序总是通过比较返回0

We can double-check this assumption by analyzing the next few lines: 我们可以通过分析接下来的几行来仔细检查这个假设:

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

See? 看到? It's checking for files provided at argument locations [1] and [2] - our initial assumption is right. 它正在检查在参数位置[1][2]提供的文件 - 我们的初始假设是正确的。 The program requires multiple files to be provided at the command line; 该程序需要在命令行提供多个文件; without them it cannot run. 没有它们就无法运行。 So the program exits early when it realizes it doesn't have the right number of files. 因此程序在意识到它没有正确数量的文件时就会提前退出。

// 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;
}

These lines will attempt to open the files, and return an error message and exit early if they cannot be opened (for instance, if they do not exist). 这些行将尝试打开文件,并返回错误消息并在无法打开时提前退出(例如,如果它们不存在)。

//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";
}

This code loops through the inputFile , finding numbers, and outputs them into outputFile . 此代码循环通过inputFile ,查找数字,并将它们输出到outputFile

So now we know this whole program is an exercise in reading numbers from a file and writing to another file . 所以现在我们知道整个程序是从文件读取数字并写入另一个文件的练习。 It's a simple I/O example. 这是一个简单的I / O示例。

return 0;

Remember when I said a successful program returns 0 ? 还记得我说一个成功的程序返回0吗? Well here, after all of the code has been run, the program does exactly that. 好吧,在运行所有代码之后,程序就是这样做的。

Edit: To directly answer your question, this program requires two files to be provided as filenames. 编辑:要直接回答您的问题,此程序需要提供两个文件作为文件名。 Such an example would be g++ -o fileExample fileExample.cpp input.txt output.txt where the input.txt file contains rows of numbers, and output.txt is created, sitting in the same location as both input.txt and . 这样的例子是g++ -o fileExample fileExample.cpp input.txt output.txt ,其中input.txt文件包含数字行,并且output.txt被创建,与input.txt和。在同一位置。 fileExample.cpp . fileExample.cpp

You haven't specified the correct number of arguments. 您尚未指定正确数量的参数。 Post what you entered before you hit compile. 发布您在编译之前输入的内容。 (Edit: What I said about the program's actual functionality was wrong, I skimmed through lol, the dude above summed it all up better). (编辑:我所说的关于该程序的实际功能是错误的,我浏览了大声笑,上面的老兄总结得更好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM