Catching error messages before they reach the screen

Hello.
I'm writing a program and at various points throughout the program files are opened that don't exist. Basically i'm using outFile.open(...) on a file that doesn't exist resulting in it's creation. The problem is that before it is created, a message saying "File not found - C:\...". is sent to the screen.

The program still creates the new file and writes to it just fine. I would just like to find a way to intercept this error message so that the User never sees it.

Is there some way to intercept these messages and keep them from being displayed?
That shouldn't happen. Are you using an old compiler?
No i'm using Dev-C++ version 4.9.9.2 on Windows XP.
here's the code that produces the message:

1
2
3
4
5
6
7
8
9
Path = currentUser + "\\Security.txt";
outFile.open(Path.c_str(), ios::binary);  
if(outFile.fail())
   {
   cout << "\nCannot open file! New security settings have not been saved!\n";
   Beep(1300, 100);
   system("pause");
   return;
   }


And when this code runs the file "Security.txt" does not exist. It sends the following to screen:

"File not found - User\Security.txt"

The weird thing is the error message I told it to display in case of failure never appears. My assumption is that the program creates the file instead of failing but notifies the user via the "file not found" message i'm getting.
I just need to find a way to make that message disappear.
Last edited on
Actually, if outFile.fail() isn't set, then the file WAS created...which is really strange...
Yes the file is created. outFile DOES NOT fail but why does the system display it's own message and how do i intercept it?
I think your system is behaving strangely...I've never gotten anything like that to happen before...And unless you are using a custom console window, I don't think you can intercept the message.
After letting someone take a look at my code we realized that the ouFile.open() is not causing the problem. Earlier in the function a system call is made:

system("attrib -h ...");

I forgot about this and because the file it's attempting to modify attributes on doesn't exist it sends out an error message. Sorry about that.
system("attrib -h ... >nul 2>&1");

BTW, you should avoid system() like the plague.

Hope this helps.
Last edited on
What is the ">nul 2>&1" supposed to do?
I need to find some sort of way of checking if a file exists before changing it's attributes. Any suggestions?

As for the system() calls, I know it's strongly advised to not use them but at my level i don't know how to mimic those system() calls with other code. Besides the program i'm writing is very strictly for Windows XP/Vista

I appreciate it though.
What is the ">nul 2>&1" supposed to do?
The problem is that before it is created, a message saying "File not found - C:\...". is sent to the screen.


Use the Win32 API. Google "msdn SetFileAttributes()".

Hope this helps.
Ummmm.....soo how exactly does one incorporate the Win32 API into the compiled project? lol And does the MSDN library need to be imported/included into the project somehow for this to work?

I looked at the SetFileAttributes() function and I assume what you're suggesting is embed it inside an if statement and if it returns a true value assume the file exists otherwise it doesn't. If i'm on the right track here then I also assume that in setting a file attribute with this function, failure does not mean it'll print a message to screen like the current system() call I have in place, correct?
Scratch that last post. Here's what I ended up with:

1
2
3
4
5
6
7
8
9
10
11
string Path;
bool Exist;

Path = currentUser + "\\Accounts.txt";    
Exist = SetFileAttributes(Path.c_str(), FILE_ATTRIBUTE_HIDDEN);
   
if(Exist)
   {
   Command = "attrib -h \"" + currentUser + "\\Accounts.txt\"";
   system(Command.c_str());     
   }  


So far this seems to be working as far as detecting whether a file is present and if it fails nothing shows on the screen. I'll keep you updated as i integrate it more thoroughly

Thanks again for everything!
You don't need to use system() in your last example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <windows.h>

...

bool set_file_unhidden( const string& filename )
  {
  DWORD attrs = GetFileAttributes( filename.c_str() );
  if (attrs != INVALID_FILE_ATTRIBUTES)
    {
    if (SetFileAttributes( filename.c_str(), attrs & ~FILE_ATTRIBUTE_HIDDEN ))
      return true;
    }
  return false;
  }

bool set_file_hidden( const string& filename )
  {
  return SetFileAttributes( filename.c_str(), FILE_ATTRIBUTE_HIDDEN );
  }
Thanks, I'll try and implement this instead. Is this code platform independant? I would, in the long run, like to make the entire program platform independant and as of right now 4 commands are keeping that from happeneing:

system("CLS");
system("pause");
system(MKDIR...);
system(RMDIR...);

Are there functions in the Windows.h header file (or elsewhere) that essentially accomplish the same tasks without making the program dependant upon a Microsoft OS?
Topic archived. No new replies allowed.