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: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: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
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: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: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