473,836 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
+ 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("filenam e", 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 1903
* 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.no 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::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.no 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::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++.m oderated].
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
5303
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, Mozilla 1.4 and IE6 -- all on windows XP Pro Here is the code. In all three, when I select a file, the complete path is displayed in the file input box. In Opera, reading the form field gives me the same full path. But in IE and Moz I get...
1
7060
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 file... Thank you to all of you who can help me with this one...
29
3414
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
2815
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 pc, for example PC7 has xxx-yyyy-zz1.ext PC5 has xxx-yyyy-zz2.ext PC3 has xxx-yyyy-zz8.ext
8
2153
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 line and there aren't any special characters. This is the file reading code segment that I'm using: sysFile=open(sysFilename,'r')
10
2315
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 = array( 'filename'=>'filename', 'basename'=>'filename.ext', 'extension'=>'ext', 'size'=>0,
5
1420
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 string, then close them. There's two obvious ways to do it: group your code by file, or group your code by procedure. # Method one: grouped by file. for each file: open the file, append the string, then close it
1
5613
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 execute it using tomcat server5.5. the code is as follows. if you have any other coding regarding this, please send me.it's urgent. import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;
0
1980
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 correctly so that i can allow certain indexes to be removed without completely deleting everything else. This is what i have so far. Code is below It's in the e part of the code and I have //remed the location of the for loop containg the...
0
9671
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10556
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10599
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10257
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9383
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6980
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4459
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 we have to send another system
2
4022
muto222
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.