目前我刚刚开始使用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
。出了什么问题?
哇,我永远不会试图学习这样的东西。 – john