ifstream/ofstream variable question

Here is some code that I have a question about -- It is about the Fname variable. Well I made a program that reads data in one file and outputs it to another file. Both of these files are specified by the user. My confusion lies in the Fname variable because the program asks what the in file will be, which is stored in Fname, and it also asks for the output file, which is also stored in the Fname variable as you can see if you read the code. So wouldnt there be a problem when trying to assign two different values to the one Fname variable?

The code is correct and working and was given to me by my professor. I am trying to understand how that can work? Should the second value of Fname hold the output file name? I thought of making two variables (Fname1 and Fname2) but it worked without it so I must not understand how the computer is reading the program. What am I not getting?

So to summarize: the Fname variable holds what the user types in as the input file, and then what the user types in as the output file. The program works properly and gets the data from the input file and sends to the right output file. How can this be?

Any help is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        char Quarterback [25];
	char Fname[80];
	double Att, Comp, TDs, Yards
        ifstream Input;
	ofstream Output;
	
	cout << "What is the name of the input file? ";
	cin.getline (Fname, 80);
	Input.open(Fname);
	cout << endl << "Where should the output go? ";
	cin >> ws;
	cin.getline (Fname, 80);
	Output.open(Fname);
	
Last edited on
Once the code opens the file input, you don't need the name anymore, so the second cin.getline() overwrites whatever was in Fname and replaces it, and then it opens the output file with that name.
Topic archived. No new replies allowed.