file read thru cmd

This is a discussion on file read thru cmd within the C++ Programming forums, part of the General Programming Boards category; how can i read the contents of the input.txt file thruu command line arguments?...

  1. #1
    kohatian3279
    Guest

    Question file read thru cmd

    how can i read the contents of the input.txt file thruu command line arguments?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  3. #3
    kohatian3279
    Guest

    hmmmmm!

    using visual studio!now thats more precise!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how can i read the contents of the input.txt file thruu command line arguments?
    Code:
    #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;
    }
    -Prelude
    Last edited by Prelude; 12-24-2002 at 08:42 AM.
    My best code is written with the delete key.

  5. #5
    kohatian3279
    Guest

    Unhappy

    could you explain that also?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Can you use File Streams?
    What do you want to do with the info? display it on the screen?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21