By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

file names, reading files

P: n/a
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::out);
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/ch...-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::out );

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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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::out would be wrong here.
}
Cheers, & hth.,

- Alf

Jun 27 '08 #2

P: n/a
On Apr 14, 11:48*am, "Alf P. Steinbach" <al...@start.nowrote:
* 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::out would be wrong here.

}

Cheers, & hth.,

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

Jeff
Jun 27 '08 #3

P: n/a
On Apr 14, 11:48*am, "Alf P. Steinbach" <al...@start.nowrote:
* 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::out 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
Jun 27 '08 #4

P: n/a
* Miner Jeff:
>
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?
This is an unmoderated Usenet group, there's no moderator.

The corresponding moderated group is [comp.lang.c++.moderated].
Cheers, & hth.,

- Alf
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.