hello, newbie here. Got a quick question on basic File I/O

Hey there. Before i go on, im gonna say thanks for any help in advance.

So, I want to make a file name, appended with ".dat", which is dependant on an input file earlier in the code. this ".dat" file needs to contain some information on employee's name, hourly rate, etc. This data is used again later in the program.

My question is, is it possible to name a file from a variable, as well as having a variable WITHIN the .dat file?
For instance, i have a variable named employeeName1, which is filled in by an input file. I want this employeeName1 to be the NAME of a .dat file, as well as that .dat file having the string within employeeName1 be used.

I hope i explained that well enough. Any help will be greatly appreciated.

Alright, so i figured out how to name a file using variables:

1
2
3
4
5
6
7
8
9
10
11
string employeeName1, employeeName2, employeeName3;
	ifstream masterList;
	masterList.open("master.lst");

	masterList >> employeeName1 >> employeeName2 >> employeeName3;

	ofstream employeeData1( (employeeName1+=".dat").c_str() );
	

	employeeData1.open( employeeName1.c_str() );
	


However, it only works with output files. Is there anyway to change input filenames during runtime?
ofstream is an output file. If you want an input, use ifstream.

Looks like you are opening the same file twice.
Line 7 defines and opens employeeData1, then at line 9 you try to open employeeData1 again.
ahh. I feel you.

Alright, so my question is (i might use some non-standard terminology here, so bear with me please!) how would i go about making this program "portable", so to speak?

As in, i give my hypothetical clients here this program, and they are able to edit the contents of master.lst to put in their own employee's names, as well as their own data (.dat) files? In other words, how would incorporate other master.lst files, the data file names will be different. I need determine the names of the employee files by reading data from master.lst.

Would this involve naming the .dat some variable that's in the program?

english is not my first language, so i apologize
Last edited on
Holy crapola, i think i figured it out myself!

Does this look OK?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string> 


using namespace std;

int main ()
{
	string employeeName1, employeeName2, employeeName3;
	ifstream masterList;
	masterList.open("master.lst");

	masterList >> employeeName1 >> employeeName2 >> employeeName3;



	ifstream employeeData1( (employeeName1+=".dat").c_str() );

	string employeeInformation;
	getline(employeeData1, employeeInformation, '\t');

	cout << employeeInformation;
Yes, I think that looks ok.
thank you, for what its worth hehe
Topic archived. No new replies allowed.