File processing

This is a discussion on File processing within the C++ Programming forums, part of the General Programming Boards category; This is the command line execution : animals cages.txt How do I code my C++ program to be able to ...

  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,796
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09: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

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