473,796 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constructing filename string at runtime

112 New Member
i need to open a file whose name i only know part of until runtime.

it is based on a char value.
the char represents gender and is either 'm' or ''f'

i need to load from a text file called names_m.txt or names_f.txt but i don't know until runtime.

i tried this but it didnt quite work...

Expand|Select|Wrap|Line Numbers
  1.  char fileName[18] = {"/data/names_"};
  2.   fileName[13] = gender;
  3.   fileName[14] = '.';
  4.   fileName[15] = 't';
  5.   fileName[16] = 'x';
  6.   fileName[17] = 't';
  7.   fileName[18] = '\0';
i want to use an fstream to open the file in the folder 'data' at the working directory
Oct 25 '07 #1
10 2534
Firecore
114 New Member
You couls use something like the sprintf() function.
I think that formats strings like printf except instead of printing them, it saves the string in a variable.

Look it up in MSDN for more info.

I think it mite help u.
Oct 25 '07 #2
drsmooth
112 New Member
actually i ran through it and the problem seems to be in the "/data/" part...is there something wrong with the way i access the file...

like i need to access the folder 'data' in the working directory
Oct 25 '07 #3
Studlyami
464 Recognized Expert Contributor
I would use a string as a variable (are you unable to do this for some reason).

Expand|Select|Wrap|Line Numbers
  1. string Test = "data//names_";
  2. Test += gender;
  3. Test +=".txt";
  4.  
  5. FILE * f_ptr;
  6.  
  7. f_ptr = fopen(Test.c_str(), <flags>);
  8.  
Another problem is the /data/filename

you need "data//names_" a single line is used for special operations like new line \n.
Oct 26 '07 #4
Ganon11
3,652 Recognized Expert Specialist
Studlyami, why use a FILE* in C++? If strings are available, then so are ifstreams and ofstreams.

Then again, if it's not C++ but C, then strings are not available, and you must use a char array.

Something else I see wrong is the fact that you declare filename to be 18 chars long (with indeces 0-17), but then try to use fileName[18] (which is out of bounds).
Oct 26 '07 #5
Studlyami
464 Recognized Expert Contributor
I guess because its what I'm comfortable with. If you can't use strings add a second / to you array and change the size appropriately and it should work.
Oct 26 '07 #6
oler1s
671 Recognized Expert Contributor
I guess because its what I'm comfortable with.
That's a poor excuse. You'll turn everything into a nail because you can only use a hammer?
Oct 26 '07 #7
Studlyami
464 Recognized Expert Contributor
I know it is. It was more of a problem of ignorance than laziness. Thats one of the reasons why i stay around this site. Theres always room for improvement.
Oct 26 '07 #8
drsmooth
112 New Member
could someone maybe show me an example of this with the fstream or ifstream?

i tried this:

Expand|Select|Wrap|Line Numbers
  1. void Civilian::assignName()
  2. {
  3.  string test = "data//names_";
  4.  test += gender;
  5.  test +=".txt";
  6.  fstream file(test, ios::in);
  7.  string g;
  8.  file>>g;
  9.  cout<<g;
  10. }
but i must have set up the fstream wrong i got:

[C++ Error] Civilian.cpp(36 ): E2285 Could not find a match for 'fstream::basic _fstream(string ,enum)'
Oct 26 '07 #9
Studlyami
464 Recognized Expert Contributor
As you read above i havn't used ifstream or ofstream, but here is a reference on how to use these two streams .
Oct 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3027
by: nfr | last post by:
I have a Singleton Model object that can be instantiated by different applications at runtime. This object activates a Channel using the config file in its constructor, which needs the name of the config file as a string. Because the object does not know the application in which it is being instantiated and because it's constructor is private, I need to know how to determine the name of the default config file (i.e.,...
10
13080
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly generated number from a program that I saved from. So I need to find a way that I can quickly remove the underscore and the last 4 numbers from the filename without changing anything else. I can't set a max length to the file becuase the filename's...
3
1250
by: Crouchie1998 | last post by:
On the following page is for Symantec Antivirus updates: http://www.sarc.com/avcenter/download/pages/US-SAVCE.html Todays virus definition filename & path is: http://definitions.symantec.com/defs/20050228-024-x86.exe Notice the filename is: 20050228-024-x86.exe
11
1920
by: bgreen | last post by:
I am hoping for some assistance with formatting a large text file which consists of a series of individual records. Each record includes specific labels/field names (a sample of 1 record (one of the longest ones) is below - at end of post. What I want to do is reformat the data, so that each individual record becomes a row (some cells will have a lot of text). For example, the column variables I want are (a) HD in one column (b) BY in...
5
1788
by: Petrosky | last post by:
Hello, May I ask what built-in functions available out there in C# to search for a particular filename input from the user at runtime ? I am completely new to system thingies, so please help beginner. I appreciate any answers from you.
1
6404
by: herbert | last post by:
I am using a TraceSource object configured via app.config. In order to delete the old file before starting the new trace, I need to obtain the filename from the listener. What am I doing wrong in the following sample: Dim trc As New TraceSource("HappySource") ‘filename returns Nothing: Dim filename As String = _ trc.Listeners.Item("myTextListener").Attributes("initializeData") app.config part:
3
1135
by: David Thielen | last post by:
Hi; I have event logging working now (thank you all for the help). But we now need to add doing this to our setup program. The file on my system is: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll So my question is how do we programatically get this path and filename? 1) Where do we get the C:\WINDOWS\Microsoft.NET\Framework part from? Do we just add Microsoft.NET\Framework to the windows directory? 2) The...
1
2502
by: =?Utf-8?B?QnJpYW4=?= | last post by:
I want to convert a filename to it's equivalent unc filename. First, is there a C# call that i can use that will do it directly? If not then I will look at the Directory.FullPath() of a file and detemine it it has a VolumeSeparatorCharacter in it. If so I will then look at the DriveInfo for that volume and determine if it is a network drive. If not then I will replace the volume specifier with a unc name like \\192.168.1.2\c$\... ...
1
2120
by: James Hahn | last post by:
I have a ListView that displays files selected from various folders using a user-specified selection rule. I want to sort the files in various ways and have implemented a sorter class to do it, for instance to sort the file date as a date and not as text. But the filenames are not listed in the same sequence as in Explorer, which is some sort of 'Numeric-modified alpha' sequence. Is there a comparer that will return the correct sort...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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
10168
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
9047
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
6785
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
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.