Hi,
Dear experts,I have a question about open() function of gcc3.4.6.I write a example:
using namespace std;
#include<iostream>
#include<fstream>
int main()
{
std::ofstream txt_stream;
int status=0;
char txt_stream_file[100];
const char *file;
if(txt_stream==NULL)
// if(!txt_stream.is_open())
cout<<"cannot open "<<file<<endl;
exit(0);
}
cout<<" OK"<<endl;
return status;
}
It can be compiled and ran.The statement file=txt_stream_file is in order to
change char* to const char*. But open() function is not work,and out:
can not open plots/results.txt
The open() function of gcc3.4.6 is:
/**
* @brief Opens an external file.
* @param s The name of the file.
* @param mode The open mode flags.
*
* Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
* function fails, @c failbit is set in the stream's error state.
*
* Tip: When using std::string to hold the filename, you must use
* .c_str() before passing it to this constructor.
*/
void
open(const char* __s,
ios_base::openmode __mode = ios_base::out | ios_base::trunc)
{
if (!_M_filebuf.open(__s, __mode | ios_base::out))
this->setstate(ios_base::failbit);
}
It is void function,and how can I know the function is work.For calling open()
function succesfully,what can I do in my code.Could please experts give me more
advice.
wl wang
I copied your codes and compiled,ran it.But don't open the file in my computer system(redhat linux enterprise4.8/gnu gcc3.4.6).I think I can't use the std::string,because the original codes is:
if(txt_stream==NULL)
{
cout<<"can not open "<<txt_stream_file<<endl;
exit(0);
}
txt_stream<<" Results from galplot"<<endl; //AWS20051124
txt_stream<<txt_stream_file<<endl;
cout<<" <<<<set_output "<<endl;
return status;
}
This is galprop package that is wrote by Strong and Moskalenko.All codes is OK,but open() function is not work in my computer.For calling open function what can I do in my codes and how convert char* into const char*.
It is also bad form to exit() from a C++ program. It will prevent some destructors from being called. That will cause problems if those objects manage persistent system resources such as temp files, message queues or shared memory. Use exceptions and always return from main().
I did follow your advice,compiled it and ran,out:
cannot open plots/results_50_599278_l_0.25_29.75_330.25_359.75_b_-4.75_-0.25_0.25_4.75_unconv_porter_isrf_July11_new9rings.txt: No such file or directory
Could please yu tell me what can I do.