Jump to content
  • Advertisement
Sign in to follow this  
sjmx22

works yet returns an error: C++

This topic is 3610 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   ifstream infile( "in_file.txt" );
   ofstream outfile( "out_file.txt" );
   
   if ( ! infile )
   {
        cerr << "error: unable to open input file!\n";
		return -1;
   }

   if ( ! outfile )
   {
        cerr << "error: unable to open output file!\n";
		return -2;
   }
   
   string word;
   while ( infile >> word )
           outfile << word << ' ';
   return 0;
}

It definitely reads the infile and outputs it to the outfile, but it still returns the unable to open file error. Can someone tell me why?

Share this post


Link to post
Share on other sites
Advertisement
Step through your code with the debugger, and determine what happpens. Also make sure that you delete the output file before testing.

Share this post


Link to post
Share on other sites
What about using is_open() ? e.g:

if ( !infile.is_open() )
...
if ( !outfile.is_open() )

Share this post


Link to post
Share on other sites

'file-input-output.exe': Loaded 'C:\Documents and Settings\Comp\My Documents\Visual Studio 2008\Projects\C++ Primer\Debug\file-input-output.exe', Symbols loaded.
'file-input-output.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'file-input-output.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'file-input-output.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcp90d.dll'
'file-input-output.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll'
The program '[3484] file-input-output.exe: Native' has exited with code -1 (0xffffffff).


If i take out the error checking on infile it returns 0, so it doesn't have a problem with outputting, and the code is the same so that can't be it then, so I really don't know.

When I debug it stops at the error and doesn't write to the file, yet if I run the exe in the folder it gives me the error and writes to the file. I'm at a lose as to how it could think it can't open the file yet still write from it.

It does this with, is_open() or the way I have it in my original post.

Share this post


Link to post
Share on other sites
Have you considered that the program may be looking for the input file in a different folder than you think? (I think this might be different between Debug and Release builds, and perhaps whether you run the program from the IDE or not).

Anyway, if the file couldn't be opened


while ( infile >> word )


this wouldn't do anything, evaluate to false and skip the loop. (If you are seeing an expected output file, perhaps it already exists from a previous successful run?)

Share this post


Link to post
Share on other sites
Quote:

Have you considered that the program may be looking for the input file in a different folder than you think? (I think this might be different between Debug and Release builds, and perhaps whether you run the program from the IDE or not).

Anyway, if the file couldn't be opened



while ( infile >> word )



this wouldn't do anything, evaluate to false and skip the loop. (If you are seeing an expected output file, perhaps it already exists from a previous successful run?)


I have the input file in the Debug folder, that is where it sticks the exe that I can manually run without the IDE. I would assume that is the right folder? Either way, that doesn't explain why when the exe is ran without going through debug that it still obviously opens the file and writes it to the output file, and still returning an error saying it couldn't open it.

I clear the output file everytime I do it, and it consisently outputs and returns the error when I run the exe without being in the IDE. Debugging through the IDE just returns the error code and stops without writing to the file.

Share this post


Link to post
Share on other sites
Quote:
Original post by sjmx22
*** Source Snippet Removed ***

It definitely reads the infile and outputs it to the outfile, but it still returns the unable to open file error.

Can someone tell me why?


Uninitialized data.

failbit = The last input operation failed because of an error related to the internal logic of the operation itself.

Errorbit = Error due to the failure of an input/output operation on the stream buffer.

no last io operation = invalid answer from things that depend on the last io operation.

Share this post


Link to post
Share on other sites
Quote:

Either way, that doesn't explain why when the exe is ran without going through debug that it still obviously opens the file and writes it to the output file, and still returning an error saying it couldn't open it.


It is impossible that a program would execute any code in main after it has returned from it. If that is so your compiler must be terribly broken. (May-be you should try rebuild all?)

The only thing that should happen is that the output file would be created regardless of whether opening the input file succeeded.

Share this post


Link to post
Share on other sites
The program wasn't looking into the right directory for some reason, there was nothing wrong with the code. I fixed it.

Thanks for the help everyone. Appreciate it.

[Edited by - sjmx22 on August 4, 2008 6:46:26 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!