473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

constructing filename string at runtime

112 100+
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 2497
Firecore
114 100+
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 100+
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 Expert 256MB
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 Expert 2GB
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 Expert 256MB
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 Expert 512MB
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 Expert 256MB
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 100+
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 Expert 256MB
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
weaknessforcats
9,208 Expert Mod 8TB
[C++ Error] Civilian.cpp(36): E2285 Could not find a match for 'fstream::basic_fstream(string,enum)'
Did you include <fstream>??
Oct 28 '07 #11

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

Similar topics

3
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...
10
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...
3
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: ...
11
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...
5
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...
1
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...
3
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:...
1
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...
1
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.