the open() function of gcc3.4.6(redhat linux enterprise4.8)

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;

strcpy(txt_stream_file,"plots/");
strcat(txt_stream_file,"results");
strcat(txt_stream_file,".txt");
file=txt_stream_file;
txt_stream.open(file,std::ios::out);

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

th12,August,2010
If you reorganise your instructions, use std::string for strings, and construct the stream with the filename, it gets much simpler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string filename;
    filename = "plots/";
    filename += "results";
    filename += ".txt";

    std::ofstream txt_stream(filename.c_str());
    if (!txt_stream)
    {
        std::cout << "cannot open " << filename << std::endl;
        return 1;
    }

    std::cout << "ok" << std::endl;
    return 0;
}
Last edited on
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:

#include"Galplot.h"
#include"galprop_classes.h"
#include"galprop.h"

int Galplot::set_output()
{
cout<<" >>>> set_output "<<endl;
int status=0;
char name[100],canvastitle[100], workstring1[100],workstring2[100];

//char txt_stream_file[400];
//============== postscript, gif and txt files

sprintf(workstring1,"l_%.2f_%.2f_%.2f_%.2f", //AWS20040315
galplotdef.long_min1,galplotdef.long_max1,
galplotdef.long_min2,galplotdef.long_max2);

sprintf(workstring2,"b_%.2f_%.2f_%.2f_%.2f", //AWS20040315
galplotdef.lat_min1,galplotdef.lat_max1,
galplotdef.lat_min2,galplotdef.lat_max2);

strcpy(txt_stream_file,"plots/");
strcat(txt_stream_file,"results_");
strcat(txt_stream_file,galdef.galdef_ID);
strcat(txt_stream_file,"_" );
strcat(txt_stream_file,workstring1);
strcat(txt_stream_file,"_" );
strcat(txt_stream_file,workstring2);

if(galplotdef.convolve_EGRET==0)
strcat(txt_stream_file,"_unconv_" );
if(galplotdef.convolve_EGRET!=0)
strcat(txt_stream_file,"_conv_" );
strcat(txt_stream_file,galplotdef.psfile_tag);
strcat(txt_stream_file,".txt"); //AWS20051124
cout<<txt_stream_file<<endl; //AWS20051124
txt_stream.open(txt_stream_file); //AWS20051124

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*.
Last edited on
I copied your codes and compiled,ran it.But don't open the file in my computer system

I think you're specifying the wrong path/filename.
The test for txt_stream == NULL is bad form. Just use if (!txt_stream). txt_stream is not a pointer, so you really should not compare it to one.

When the open() fails on Linux, errno will be set and you can see the error message by doing

1
2
3
4
5
if(!txt_stream)
{
    cerr << "Error opening " << txt_stream_file << ": " << strerror(errno) << endl;
    return 1;
}


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.
Last edited on
Create the file it's looking for? Or use an absolute path rather than a relative one?
Yeah,it's OK.Thank you very much.
Topic archived. No new replies allowed.