File I/O Problem

This is a discussion on File I/O Problem within the C++ Programming forums, part of the General Programming Boards category; When I try to open a file it says it doesn't exist, but it really does though.. Reader( string fileToRead ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    File I/O Problem

    When I try to open a file it says it doesn't exist, but it really does though..

    Reader( string fileToRead );

    // To simplify the implementation, the user may either call "getLine" but
    // not "hasAnotherOpenAngledBracket" and "getToken" OR
    // call "hasAnotherOpenAngledBracket" and "getToken" but not "getLine".

    // Reads the next input line and stores it in "line". It returns ture
    // if "read" did not return eof. Otherwise, it returns false.

    Code:
    #include<iostream>
    #include <string>
    #include "Reader.hpp"
    
    // Copy the contents of "inputFileName" to standard output.
    // This program demonstrates the use of "Reader" and its
    // function, "getLine".
    int main( int argc, char *argv[] )
    {
    
            if ( argc != 2 ) {
    
                    cout<< "usages: "<< argv[0] <<" <filename>\n\n";
                    exit(0);
            }
    
     Reader reader(argv[1]);
    
    
    return 0;
    }
    Code:
    #include <iostream>
    #include "Reader.hpp"
    #include <fstream>
    using namespace std;
    
    Reader::Reader( string fileToRead)
       {
       ifstream inFile;
    
    
       inFile.open("fileToRead");
    
       if(!inFile.is_open())
         {
         cout << "Error: " << fileToRead  << " does not exitst!" << endl;
        exit(0);
         }
    
       inFile.close();
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by gqchynaboy
    When I try to open a file it says it doesn't exist, but it really does though..
    Ah -- but does it exist in the current working directory? Try entering the full path to the file on the command-line when you run the program. fstream.open() will not search your hard drive for you to find the file, it either wants the full path or wants the file in the program's current working directory.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> inFile.open("fileToRead");

    That code is attempting to open a file called "fileToRead" and is ignoring the input parameter of the same name. Whatever you are adding to the comand line is being ignored. It should be:

    inFile.open(fileToRead.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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