Thread: File processing

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    10

    File processing

    This is the command line execution :

    animals cages.txt

    How do I code my C++ program to be able to read in the argument as my text file??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <fstream>
    
    using namespace std;
    
    int main ( int argc, char **argv )
    {
      if ( argc > 1 ) {
        ifstream fin ( argv[1] );
        if ( !fin ) {
          // Probably a bad file name
        }
        else {
          // Use fin
        }
      }
    }
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    See the FAQ
    The first command line parameter is pointed to by argv[1]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    thanks people.

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. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM