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?
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.
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.
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
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?
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, 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:
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?