Hello all.

I am working on a programming 2 lab and am unsure how to code the input and output for this assignment. Here is what the lab sheet says:

"Your program will be expected to read input from the first file specified on the command line and to write output to the second file specified on the command line. Your program should display an error message and exit immediately if the expected command-line parameters are not provided. Following is an example of how your program will be run from the command line:
$ ./main input.txt output.txt
Your program will continue reading and processing commands from the input file until the end of the file is reached. After processing each command, your program should write the result to the output file. All input and output should use C++ streams (>> and <<).
For this lab, strings may be stored in static-sized character arrays. You can assume that your program will not be tested with any strings longer than 100 characters, and all strings will be separated by whitespace. "

With this criteria i am unsure on how to write my main function. Any help would be appreciated.

What part(s) of the assignment don't you know how to do? main() has two optional parameters -- argc and argv. int main(int argc, char* argv[]) argc is the number of parameters on the command line -- normally it is a minimum of 1, but on some embedded systems it could be 0.

argv is an array of the arguments.

In the example you posted,
argv[0] == main
argv[1] == input.txt
argv[2] == output.txt

no just create an ifstream and an ofstream object using argv[1] and argv[2] as the filenames.

Hello all.

I am working on a programming 2 lab and am unsure how to code the input and output for this assignment. Here is what the lab sheet says:

"Your program will be expected to read input from the first file specified on the command line and to write output to the second file specified on the command line. Your program should display an error message and exit immediately if the expected command-line parameters are not provided. Following is an example of how your program will be run from the command line:
$ ./main input.txt output.txt
Your program will continue reading and processing commands from the input file until the end of the file is reached. After processing each command, your program should write the result to the output file. All input and output should use C++ streams (>> and <<).
For this lab, strings may be stored in static-sized character arrays. You can assume that your program will not be tested with any strings longer than 100 characters, and all strings will be separated by whitespace. "

With this criteria i am unsure on how to write my main function. Any help would be appreciated.

You're going to have to be more explicit on the exact question.

Step 1 - Check argc to make sure there is the correct number of parameters. Display error message and exit if not.

Step 2 - Extract input filename from argv[] array.

Step 3 - Extract output filename from argv[] array.

Step 4 - Create ifstream and ofstream from input and output filenames. Display error and exit if this fails.

Step 5 - Read command from input file/ifstream.

Step 6 - Process command.

Step 7 - Send result to output file/ofstream.

Step 8 - Repeat steps 5 through 7 for each command.

Which step(s) are you having problems with?

You're going to have to be more explicit on the exact question.

Step 1 - Check argc to make sure there is the correct number of parameters. Display error message and exit if not.

Step 2 - Extract input filename from argv[] array.

Step 3 - Extract output filename from argv[] array.

Step 4 - Create ifstream and ofstream from input and output filenames. Display error and exit if this fails.

Step 5 - Read command from input file/ifstream.

Step 6 - Process command.

Step 7 - Send result to output file/ofstream.

Step 8 - Repeat steps 5 through 7 for each command.

Which step(s) are you having problems with?

Actually steps 2 through 6. I'm a bit rusty with my c++ code. I did as Ancient Dragon

ifstream argv[1];

and

ofstream argv[2];

but i got an error "error: declaration of 'std::ifstream argv [1]' shadows a parameter. 'argv' has a previous declaration as 'std::ifstream argv [1]' ".

Actually steps 2 through 6. I'm a bit rusty with my c++ code. I did as Ancient Dragon

ifstream argv[1];

and

ofstream argv[2];

but i got an error "error: declaration of 'std::ifstream argv [1]' shadows a parameter. 'argv' has a previous declaration as 'std::ifstream argv [1]' ".

You need to give your ifstream and ofstream variable names and they can't be argv , since that's taken already. Often they are called ins and outs . So declare them, then open them with the open command and put the relevant filenames inside the parentheses:

ifstream ins;
    ins.open (argv[1]);
    ofstream outs;
    outs.open (argv[2]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.