output file name

say i have this code...

1
2
3
4
5
6
7
8
9
 ifstream fin("test.txt");     
	int charArray [128];
	for (int i = 0; i < 128; ++i)
		charArray [i] = 0;

	if (fin.is_open());
	{
		cout << "File opened. \n";
		while (!fin.eof() )


how can I cout << the name of the inFile?

cout the name of he inFile


Do you mean like outputing the phrase "test.txt"?



Just in case you're wondering. Neither the istream or ostream classes save the name of your filename. So you can not do anything like this.
1
2
ifstream fin("test.txt");
cout << fin.getFileName(); // This does not exist. 
Last edited on
yes..
so, if someone changes the inFile name, the cout will still output the name of the inFile
You'll have to save the name of the textFile in a another variable.

I'm not going to lie. This can be kind of tricky. So you need to be very specific with me. I'll explain to you why in a moment.

1.) Is "text.txt" the only file name you want to use? (AKA is it going to be constant)

2.) How is "test.txt" set? Does the user input the file name, or is it hardcoded in (I.E. inFile = "text.txt" vs cin >> inFile)

EDIT:
Why does this matter? Because the operators "=" and ">>" will not work unless the conditions are correct.
Last edited on
1. Yes

2. It is hard coded by whoever I give the program to... not cin

I just wanted to add another feature to the program.
It's really not important... just pretty.
That's not too bad

1
2
char * inFile = "text.txt";
ifstream(inFile);



I was actually probably a little panicky. I had some nightmares with char arrays back in my day.
Last edited on
Topic archived. No new replies allowed.