CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    5

    How do I handle this error with ifstream and ofstream??

    I make a simple program with file I/O.
    So I use ifstream and ofstream.
    So I coded
    std::ifstream file_in("input.txt");
    std:fstream file_out("output.txt");
    (input.txt and output.txt is at same dir that contains my program)
    .
    But when my program run, it can`t open that files.

    HOW DO I HANDLE THIS ERROR???

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How do I handle this error with ifstream and ofstream??

    Check that the file names are correct, e.g., if you have common file extensions hidden by default, the file that you think is named "input.txt" might actually be named "input.txt.txt".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: How do I handle this error with ifstream and ofstream??

    This is the IO syntax I use,
    Code:
       std::string input_file, output_file;
    
       ifstream input_file_stream; 
       ofstream output_file_stream;
    
       //use .open() and pass the file name strings as c_str() 
       input_file_stream.open( input_file.c_str() );
       output_file_stream.open ( output_file.c_str(), ofstream::out );
    
       std::string my_first_line_of_output, my_second_line_of_output;
    
       // add to the output stream, std::endl adds end of line char
       output_file_stream << my_first_line_of_output << std::endl;
       output_file_stream << my_second_line_of_output << std::endl;
    
       // don't forget to close the streams
       input_file_stream.close();
       output_file_stream.close();
    This is sort of pseudocode in that the order of instructions is not thought out, but everything should be here and the syntax should be right. Generally you put the data from the input file in a data structure, you may have to parse it, etc.

    The use of a string for your input and output file names allows you to pass the name in an argument etc.
    Code:
    // declare these first
    std::string input_file, output_file;
    
    while ((cmdArguments = getopt (argc, argv, "i:o")) != -1)
       switch (cmdArguments)
       {
          case 'i':
             input_file = optarg;
          break;
          case 'o':
             output_file = optarg;
          break;
       }
    // run your app with ./app.exe -i input_file -o output_file

    Also make sure you have included the right header files. You will need string and fstream at least.

    It would be helpful if you would post the compiler error message(s) and a bit more of your code if possible.

    LMHmedchem
    Last edited by LMHmedchem; May 18th, 2011 at 11:45 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How do I handle this error with ifstream and ofstream??

    Quote Originally Posted by LMHmedchem
    This is the IO syntax I use
    If you can initialise the ifstream and ofstream, you should do so instead of calling open(). If those stream objects are soon to go out of scope, you might as well let that happen instead of calling close().

    Quote Originally Posted by LMHmedchem
    It would be helpful if you would post the compiler error message(s)
    Since the program ran, there aren't any error messages from the compiler, though there might be warnings.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: How do I handle this error with ifstream and ofstream??

    So you are saying do,
    Code:
    std::string input_file
    ifstream input_file_stream( input_file.c_str() );
    instead of calling open? What is the reason for this, and when do you use open?

    I generally use this for small programs, where the fstreams are in main() and don't go out of scope, so close is generally in there for me. I don't know what OP has in mind for how to use this.

    It seems like it would be a good idea to code up common functions like open_file(), perhaps that returned a pointer to the ifstream or something like that. Then you could just include the code in an include file every time you need it.

    LMHmedchem

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How do I handle this error with ifstream and ofstream??

    Quote Originally Posted by LMHmedchem
    So you are saying do,
    Code:
    std::string input_file
    ifstream input_file_stream( input_file.c_str() );
    instead of calling open? What is the reason for this, and when do you use open?
    Yes. The idea is to declare variables near first use and provide them with a sensible initial value. So, if you can create a stream object with the appropriate constructor, you might as well do so instead of default constructing it and then calling open(). But if the stream object has already been constructed, then open() may need to be used.

    Quote Originally Posted by LMHmedchem
    I generally use this for small programs, so the fstreams are in main() and don't go out of scope, so close is generally in there for me.
    They go out of scope when control leaves the main function.

    Quote Originally Posted by LMHmedchem
    It seems like it would be a good idea to code up common functions like open_file(), perhaps that returned a pointer to the ifstream or something like that. Then you could just include the code in an include file every time you need it.
    The stream constructor I'm suggesting already does open_file().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured