file names, reading files

Discussion in 'C++' started by Miner Jeff, Apr 14, 2008.

  1. Miner Jeff

    Miner Jeff Guest

    Hello, I have a basic question about reading files.

    I have several data files where the filenames are identical except for
    a short (3 character) prefix. I inherited this code and the person who
    developed it was making a duplicate of each file and then deleting the
    prefix on the copied file so the following statement could read a
    generic "filename":



    ifstream inFile("filename", ios::eek:ut);


    There are 20 files so I'd like to avoid this manual operation each
    time I run the program.

    I want to add a prefix to the string "filename" before reading the
    file so I don't have to go manually create the file before running the
    program. Each time I run the program, I want the operator to enter
    the Prefix and then have the program combine the prefix with
    "filename" and read the file.

    It's not often I get to test code on my target system so I'm asking
    for opinions on the following approach. Part of the following code was
    gleaned from a posting by Ralph McArdell at:
    http://en.allexperts.com/q/C-1040/changing-name-text-file.htm . I'm
    only reading the file once during program execution so I don't think I
    need to rewind os as was shown in the referenced posting.


    ostringstream os;

    char prefix[3]="";
    cin>>prefix;
    os << prefix << "filename";
    ifstream infile( os.str().c_str(), ios::eek:ut );



    I appreciate any comments and suggestions. If there's a way to
    concatenate the operator-entered 'prefix' with 'filename' without
    using os, that's what I'd prefer.


    Thanks,

    Jeffrey Bledsoe
    Miner Jeff, Apr 14, 2008
    #1
    1. Advertising

  2. Miner Jeff

    Miner Jeff Guest

    On Apr 14, 11:48 am, "Alf P. Steinbach" <> wrote:
    > * Miner Jeff:
    >  > [how to concatenate strings]
    >
    > #include <string>
    > #include ...
    >
    > int main()
    > {
    >      using namespace std;
    >      string const basicFilename = "pasta.food";
    >      string prefix;
    >
    >      getline( cin, prefix );   // Error checking omitted for brevity.
    >      string const fullFilename = prefix + basicFilename;
    >
    >      ifstream infile( fullFilename.c_str() );  // ios::eek:ut would be wrong here.
    >
    > }
    >
    > Cheers, & hth.,
    >
    > - Alf


    Thanks Alf. I'll try it tomorrow during my scheduled test time.

    Jeff
    Miner Jeff, Apr 14, 2008
    #2
    1. Advertising

  3. Miner Jeff

    Miner Jeff Guest

    On Apr 14, 11:48 am, "Alf P. Steinbach" <> wrote:
    > * Miner Jeff:
    >  > [how to concatenate strings]
    >
    > #include <string>
    > #include ...
    >
    > int main()
    > {
    >      using namespace std;
    >      string const basicFilename = "pasta.food";
    >      string prefix;
    >
    >      getline( cin, prefix );   // Error checking omitted for brevity.
    >      string const fullFilename = prefix + basicFilename;
    >
    >      ifstream infile( fullFilename.c_str() );  // ios::eek:ut would be wrong here.
    >
    > }
    >
    > Cheers, & hth.,
    >
    > - Alf


    It worked. I decided to create a folder for the files so I created
    another string constant (for the directory) and concatenated it with
    the other 2 strings. Thanks for the help.

    BTW, I was surprised to see, judging from the subject lines, a lot of
    off-subject 'trash' on this site today. I didn't notice anything of
    that nature yesterday. I assume the discussion manager cleans it up
    once in a while. Correct?

    Thanks again,

    Jeff
    Miner Jeff, Apr 15, 2008
    #3
    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. Satish Kumar Chimakurthi

    Reading files with names dynamically changing

    Satish Kumar Chimakurthi, Feb 4, 2004, in forum: Python
    Replies:
    2
    Views:
    271
    Miki Tebeka
    Feb 4, 2004
  2. fBechmann
    Replies:
    0
    Views:
    393
    fBechmann
    Jun 10, 2004
  3. Replies:
    0
    Views:
    774
  4. Replies:
    7
    Views:
    649
    Lyell Haynes
    May 2, 2006
  5. Sfdesigner Sfdesigner
    Replies:
    5
    Views:
    157
    Chris Shea
    Aug 13, 2007
Loading...

Share This Page