Just a syntax question, here is my code snippet. (Sorry, browser isn't letting me paste properly into stack overflow.)
#include <iostream> /* 'iostream.h' is deprecated. */
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std; /* Required. */
FILE *OpenFile(char *Filename)
{
FILE *FP;
if((FP = fopen(Filename, "r")) == NULL)
{ /* Error opening file. */
std::cout << "[!!] Unable to open database!"
<< " Are you sure it exists?\n"
<< "[<<] Database Unchanged.\n";
exit(EXIT_FAILURE); /* End program. */
}
else /* Properly opened the file. */
return FP;
}
int main(void)
{
FILE *Data; /* Our database file pointer. */
Data = OpenFile("Data.txt");
printf("Success!\n");
return 0;
}
When I compile, I get the following warning:
$ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:27:28: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
$
Where am I going wrong?
cerr; that's what it is designed for. And, in theory, it would be a good idea to useFILE *Data = OpenFile("data.txt");and to close the file before exiting. In practice, in this context, it does not matter much, but in many contexts, not freeing allocated resources is a bad idea, and RAII (Resource Acquisition Is Initialization) suggests the initialize on definition notation.