Simple string manip question

Discussion in 'C++' started by Lorn, Jun 29, 2005.

  1. Lorn

    Lorn Guest

    I'm trying to add a file tpye to the end of a string as I'm doing some
    file writing. This code doesn't work, what would be the correct way of
    doing this:

    ofstream a_file( myString + ".txt", ios::app );

    Sorry for the triviality, I've been searching around all morning to no
    avail. Thanks, for any help.

    Lorn
    Lorn, Jun 29, 2005
    #1
    1. Advertising

  2. Lorn

    Son of Sam Guest

    do this:
    string a,b;
    string bambi = a + b;

    fstream a_file;
    a_file.open(bambic_str(), ios_base::in | ios_base::eek:ut | ios::binary);
    Son of Sam, Jun 29, 2005
    #2
    1. Advertising

  3. Lorn

    Lorn Guest

    thanks, SOS

    regards, lorn
    Lorn, Jun 29, 2005
    #3
  4. Lorn wrote:
    > I'm trying to add a file tpye to the end of a string as I'm doing some
    > file writing. This code doesn't work, what would be the correct way of
    > doing this:
    >
    > ofstream a_file( myString + ".txt", ios::app );


    What is myString? And how does your code "not work" - what error
    message do you get?

    If myString is a char*, then + doesn't work at all.

    If myString is a string, then + will do what you expect and return a string.

    But the ofstream constructor requires a char* for the filename
    parameter, not a std::string (aarrgghh!). So you need to get a char*
    for that temporary string:

    (myString+".txt").c_str()

    --Phil.
    Phil Endecott, Jun 29, 2005
    #4
  5. Lorn

    Son of Sam Guest

    >do this:
    >string a,b;
    >string bambi = a + b;


    >fstream a_file;
    >a_file.open(bambic_str(), ios_base::in | ios_base::eek:ut | ios::binary);


    here i meant: a_file.open(bambi.c_str(), ios_base::in | ios_base::eek:ut |
    ios::binary);
    (the dot before .c_str() )
    Son of Sam, Jun 30, 2005
    #5
  6. Lorn

    Lorn Guest

    It was actually a CString. I ended up doing the code below, which works
    fine. Thank you guys for your help.

    CString name = "myName"
    CString filetype = ".txt";
    CString filename = name + filetype;
    ofstream a_file ( filename, ios::app );
    Lorn, Jul 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blupp
    Replies:
    0
    Views:
    1,001
    blupp
    Jan 15, 2004
  2. marcie
    Replies:
    0
    Views:
    581
    marcie
    Apr 28, 2004
  3. News M Claveau /Hamster-P

    .zip files sample manip

    News M Claveau /Hamster-P, Aug 1, 2003, in forum: Python
    Replies:
    2
    Views:
    348
    News M Claveau /Hamster-P
    Aug 5, 2003
  4. J. Cooper
    Replies:
    2
    Views:
    135
    J. Cooper
    Mar 5, 2008
  5. Louis Erickson
    Replies:
    2
    Views:
    197
    James Willmore
    Sep 3, 2003
Loading...

Share This Page