-
verify a file opened
I'm writing a program and have to verify that the file open command did succeed
and if not the program will write a message to that effect and then terminate
then we have to prompt the user for strings of data until the user wants
to quit. Any suggestions on this would be greatly appreciated.
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
main()
{ ofstream outPutFile("ATxtFile.dat", ios::app);
char our_String[80];
cout << "We will be adding data to textfile :";
cout << "\n\nENTER a string (press ENTER only to quit): "
<< endl;
cin.getline(our_String, 80, '\n');
outPutFile << our_String
<< endl;
outPutFile.close();
return 0;
}
-
Re: verify a file opened
Its a boolean I think, maybe i am thinking FILE*, I forget.
ofstream x;
if(x.open("filename")
{
do ok stuff
}
else
{
do not ok stuff.
}
"newatit" <janraeevans@aol.com> wrote:
>
>I'm writing a program and have to verify that the file open command did
succeed
>and if not the program will write a message to that effect and then terminate
>then we have to prompt the user for strings of data until the user wants
>to quit. Any suggestions on this would be greatly appreciated.
>
>
>#include <iostream.h>
>#include <fstream.h>
>#include <iomanip.h>
>#include <stdlib.h>
>#include <conio.h>
>
>main()
>{ ofstream outPutFile("ATxtFile.dat", ios::app);
>
>
> char our_String[80];
>
> cout << "We will be adding data to textfile :";
>
> cout << "\n\nENTER a string (press ENTER only to quit): "
> << endl;
> cin.getline(our_String, 80, '\n');
>
>
> outPutFile << our_String
> << endl;
>
>
> outPutFile.close();
>
> return 0;
>}
-
Re: verify a file opened
newatit wrote:
>
> I'm writing a program and have to verify that the file open command did succeed
> and if not the program will write a message to that effect and then terminate
> then we have to prompt the user for strings of data until the user wants
> to quit. Any suggestions on this would be greatly appreciated.
>
> #include <iostream.h>
> #include <fstream.h>
> #include <iomanip.h>
> #include <stdlib.h>
> #include <conio.h>
>
> main()
> { ofstream outPutFile("ATxtFile.dat", ios::app);
simply check the stream object as if it were a bool:
if(outPutFile)
{
/ successfully opened
}
else
{
//error
}
Danny