ofstream: User creates filename?

Hello!

I'm trying to automate this crazy formatting ritual I go through at the end of each month to create income statements. I need three versions of it. One for board members, IRS, etc.. - just kidding ;) The different versions will group GL amounts in differing categories to align with budget projections. My thought was to create a report object and store the mappings in text files....

So, I'm wondering if there is a way to create a file name that comes from a user input. This way I could save old versions or create new reports, etc... Plus I'm lazy and would like to cut down on the amount of code I have to write - thank you objects! :)

1
2
3
4
5
6
7
8
string rptname;
cout<<"enter rpt name."<<endl;
cin>>rptname;

ofstream fout;
fout.open(SomeConvertToFileNameFn??(rptname+".txt"));
//some code
fout.close


Maybe I'm barking up the wrong tree...

Thanks for your input!!!
Last edited on
rptname is a string, but ofstream expects a const char* as its first param.

for this reason, std::string offers the c_str() function which returns its string data in the form of a const char*:

1
2
rptname += ".txt"; // append .txt to the end of the name
fout.open( rptname.c_str() ); // <- open that file 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
    string rptname;

    // cout<<"enter rpt name."<<endl;
    cout << "enter rpt name.\n" ; // cin and cout are tied, we don't have to flush cout twice

    cin>>rptname;

    //ofstream fout;
    //fout.open(SomeConvertToFileNameFn??(rptname+".txt"));
    ofstream fout( rptname + ".txt" ) ; // string has a + operator; constructor opens the file

    //some code

    // fout.close // destructor flushes and closes the file
}
Does ofstream's ctor accept a string? I thought it only accepted a const char*?
> Does ofstream's ctor accept a string?

Yes, now it has a constructor which accepts a string (C++11).
http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream
My hero! Thank you, thank you!! It works!!

I couldn't get it to take a string but c_str() worked! Oh, the possibilities!!

Thank you both for your help!
It will be nice when professors stop instructing their students to install non-compliant C++ compilers from the 80s and 90s. Or rather, when we run out of those professors and they get replaced by next-gen professors. Mmmmmm, yummy next-gen professors.

(Though obviously the feature mentioned by JLBorges was only widely adopted a couple years ago)
Last edited on
I’m admittedly pretty amateur. I took two c++ classes, probably 7 years ago. I make mini programs to take shortcuts in my accounting life. (Very useful when you are being forced against your will to use crap software)

LB - can you recommend a good compiler?

Also, JLBorges - I appreciate the notes about flushing, but I'm not familiar with the concept. :0 Would you mind elaborating?

Thanks again everyone!
> I appreciate the notes about flushing, but I'm not familiar with the concept.

Streams have an associated stream buffer; when we write to an output stream, the bytes are held in the stream buffer instead of being immediately sent to a physical device such as stdout or a file. Eventually, when the buffer becomes full, or the stream is closed, it is 'flushed' - ie pending bytes are physically written to the device. (It would be inefficient to write each byte separately to a physical device.)

We can force an output stream to immediately flush the buffer via the member function flush(). The manipulator std::endl writes a new line to the output stream and then calls flush() on it.

By default, std::cout is tied to std::cin; this causes std::cout to be flushed before each input from std::cin

See the notes on this page: http://en.cppreference.com/w/cpp/io/manip/endl
A good compiler is one that supports the latest C++ standard, which used to be C++03, is now C++11, and will soon be C++14. Currently gcc/mingw 4.8 with the -std=c++11 flag support a good deal of C++11, and clang 3.3 with the -std=c++11 flag supports a good deal too. I recommend clang if you can.

But catching up on everything added to C++ in the last 7 years PLUS refreshing your memory will be a challenge ;)
JLBorges - I see, I vaguely remember some funkiness with getline in certain places. Maybe it was a flushing issue. I'll do some more reading. Thanks for the flushing lesson and the link! I'm all for reducing redundancy and maximizing efficiency. :)

LB - Thanks for the recommendations, I will do some compiler shopping.

Thank you both for your help, I really appreciate it!
> I vaguely remember some funkiness with getline in certain places. Maybe it was a flushing issue.

No. Flushing is associated with output streams, std::getline() operates on an input stream.

Perhaps this was the funkiness that you had encountered with getline:
http://www.cplusplus.com/forum/general/69685/#msg372532
Topic archived. No new replies allowed.