how can i read the contents of the input.txt file thruu command line arguments?
how can i read the contents of the input.txt file thruu command line arguments?
Windows:
notepad input.txt
Unix:
cat input.txt
Maybe you want to rephrase your question?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
using visual studio!now thats more precise!
>how can i read the contents of the input.txt file thruu command line arguments?
-PreludeCode:#include <iostream> #include <fstream> #include <cstdlib> #include <string> #include <vector> int main ( int argc, char **argv ) { if ( argc != 2 ) { std::cerr<<"usage: $ prog <file>"; exit ( EXIT_FAILURE ); } std::ifstream in ( argv[1] ); if ( !in.is_open() ) { std::cerr<<"error: could not open file "<< argv[1]; exit ( EXIT_FAILURE ); } std::string line; std::vector<std::string> file; while ( std::getline ( in, line ) ) file.push_back ( line ); in.close(); std::vector<std::string>::iterator it = file.begin(); while ( it != file.end() ) std::cout<< *it++ <<std::endl; }
Last edited by Prelude; 12-24-2002 at 08:42 AM.
My best code is written with the delete key.
could you explain that also?
Can you use File Streams?
What do you want to do with the info? display it on the screen?
>could you explain that also?
It's quite self documenting:
If the number of arguments is not 2 then print the usage for the program and die.
Try to open the second argument as a file, if the file isn't open then report an error and die.
As long as there's more input, read a line and add it to the end of a vector.
Close the file.
Starting at the beginning of the vector, walk to the end and print each line.
Once you have the file in a vector you can do with it what you want, I just printed the contents to stdout so that you can check to see that it works.
-Prelude
My best code is written with the delete key.
argv is an array of C style strings. The first string in argv is the name of the program. It should be there by default if you work with an IDE and don't enter any command line arguments. If you work with command line arguments, then you should put the program name first followed by any other arguments you want to pass in. Since you pass them in, you should know which sequence they are. The input file may be first, second, or third or wherever in the sequence. Likewise the output file may be listed before or after the input file. Since you know which is which you can pull the information out of argv and use it as you want.