file names, reading files

M

Miner Jeff

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
 
M

Miner Jeff

* 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
 
M

Miner Jeff

* 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top