473,473 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

file names, reading files

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
4 1875
* 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
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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joel Goldstick | last post by:
I wanted to write a simple page to let me choose a directory and then list the files in it. The end goal was to make an easy way to copy all the file names in a directory. I tested with Opera7,...
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
29
by: Tola | last post by:
In my case of study, my teacher gave me a project, after I analysed the problem I found that I had to used open the file on the other machine? Please help? Thank you in advance. Tola CHROUK
4
by: Jason | last post by:
I need to open a file for reading, but I only know part of it's name. The file I want to read is in the format of xxx-yyyy-zzz.EXT The last three digits of the file name are different on each...
8
by: andrew.jefferies | last post by:
Hi, I'm trying to write a simple log parsing program. I noticed that it isn't reading my log file to the end. My log is around 200,000 lines but it is stopping at line 26,428. I checked that...
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
5
by: Steven D'Aprano | last post by:
After reading an earlier thread about opening and closing lots of files, I thought I'd do a little experiment. Suppose you have a whole lot of files, and you need to open each one, append a...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.