Reputation: 43
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. 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
. What is going wrong?
Upvotes: 1
Views: 83
Reputation: 45
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).
Upvotes: 0
Reputation: 2615
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.
#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.
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.
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. 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
.
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.
return 0;
Remember when I said a successful program returns 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 .fileExample.cpp
.
Upvotes: 3