目前我剛剛開始使用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