2015-09-02 35 views
1

目前我刚刚开始使用C++,并希望深入了解文件I/O,以便搜索一些随机代码并键入它以查看它是否有效以及它的工作方式。但我遇到了一些我自己无法理解的问题。关于文件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; 
} 

所以我有这段代码,我编译它。它只会显示我Incorrect number of arguments。出了什么问题?

+0

哇,我永远不会试图学习这样的东西。 – john

回答

3

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

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

您应该认识到这些包含指令 - 使用与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.txtinput.txt相同的位置。 fileExample.cpp

+1

哇非常感谢。我从来没有想过,有人会这样打破这一点,并帮助我解决这个问题。我很感激并感谢你的帮助。我绝对从中学到了一些东西。 –

+1

@GoodrichClint没问题。我们在某个时候都是新手程序员。过了一段时间,您将学习以这种方式分析代码。我很高兴能够提供帮助。 – Singular1ty

+0

可能你的意思是'g ++ -o fileExample fileExample.cpp' [Enter]'./fileExample input.txt output.txt'? – bcrist

0

您尚未指定正确数量的参数。发布你在编译之前输入的内容。 (编辑:我说的关于该程序的实际功能是错误的,我通过哈哈掠过,上面的伙计总结了这一切更好)。

+0

那么这个代码实际上会起什么作用?创建一些文件并操作它们? –

+0

它希望你给它2个文件,它会显示每个文件中的条目数量。如果(argc!= 3){...}意味着如果没有确切的3个参数,程序将退出。 – Spidey

+0

会像g ++ -o helloworld helloworld.cpp file1.txt file2.txt – Spidey