Thread: Opening file using ifstream...[Urgent]

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Opening file using ifstream...[Urgent]

    i'm opening this file in my program (called astar) and the filename is an argument to the main function...so when i write out this on unix prompt (using gcc)...:
    Code:
    astar inputfile.txt
    it works fine...but my assignment says that i don't have to give the extension...cause it can be anything.... (.in, .txt etc)...so i should be able to do this:
    Code:
    astar inputfile
    however using my code the above does not work and hangs the prompt...any ideas what i should do...i'm using:
    Code:
    \\readfile function:
    void readFile (char *file)
    {
       ifstream fin (file);
    
       \\then i use fin e.g. fin >> smth;
    }
    
    \\main function:
    
    int main(int argc, char *argv[])
    {
    
       readFile (argv[1]);
    
    }

    plz help!

    Farooq

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Are checking to make sure that you actually opened the file before you try to read from it?

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If the file on disk has an extension, then you must specify that extension when you open the file. That should probably be on the command line. It doesn't really make sense to specify a filename on the command line without the extension when the actual file to be opened does have an extension.

    If that is actually what the instructor wants, you'd have to search all available files in the directory for one with that name regarless of extension... that would take a lot of work. I suggest changing the actual filename to inputfile for your input file, or changing the commandline to inputfile.txt.

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. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM