output file name

Dec 2, 2015 at 12:09am
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?

Dec 2, 2015 at 12:13am
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 Dec 2, 2015 at 12:22am
Dec 2, 2015 at 12:16am
yes..
so, if someone changes the inFile name, the cout will still output the name of the inFile
Dec 2, 2015 at 12:32am
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 Dec 2, 2015 at 12:36am
Dec 2, 2015 at 1:25am
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.
Dec 2, 2015 at 3:34am
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 Dec 2, 2015 at 3:35am
Topic archived. No new replies allowed.