Using arrays as file names

I know how to use strings as filenames
(just use sourcefile.open(<stringname>.c_str())

but I don't know how to use char arrays as filenames,

//code fragment//

1
2
3
4
5
6
char fname[15]
// fname is a character array, already set to be some name in a previous operation

ifstream sourcefile;

sourcefile.open(fname);


when I run the program with this, it is unable to open the file.

I checked to make sure the array has the correct file name in it.

Guess:: I think I need some something extra on the "open".
Provided that you assigned fname right, and the compiler would make it hard to do it wrong, that should work. If it's failing, it's because of something other than the way you're calling open().
Perhaps post more of your code?
The entire .cpp file, no .h files or other .cpp files are used in this program.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <fstream>
#include <iostream>



class pirate {
private:
	int temp[20];
	char city[20][25]; // oh, there are 20 sets of data and the name is a maximum of 25 chars long
	int hightemp;
	int lowtemp;
	float average;
	char highname[25];
	char lowname[25];

public:
	 //I have no constructor, it caused problems so I got rid of it.

	void getfilename(char fname[25]);
	void fileread(char fname[25]);
	
	void hsearch( );
	void lsearch( );
	void asearch( );
	void printdata( );
	
};

using namespace std;

pirate data; // _pirate_ is the name of the class. data is an identifer of type _pirate_
// that was the only way I could get it to work, doing functions using "pirate" didn't seem to work.

int main()
{

	char fname[25];

	


	data.getfilename(fname);

	data.fileread(fname);

	return 0;
}


void pirate::getfilename(char fname[15])
{
	ifstream fnamefile;

	fnamefile.open("filenamehere.txt");

	fnamefile >> fname[0];


	int fcount = 0;

	while(fnamefile)
	{
		cout << fname[fcount];
		fcount++;
		fnamefile >> fname[fcount];		
	}
	return;
}




void pirate::fileread(char fname[15])
{
	
	ifstream sourcefile;

	sourcefile.open(fname);

	if(!sourcefile)
	{
		cout << "imput failure" << endl;
		return;
	}

	// the part below never gets called. It just reports abck with "imput failure"

	int count1 = 0; // keeps track of which data set
	int count2 = 0; // count of the loop to find the name.
	

	sourcefile >> reader;

	
	data.city[count1][count2] = reader;
	

	while(sourcefile)
	{
		count2++;
		while(reader != ' ')
		{
			sourcefile >> data.city[count1][count2];
			count2++;
		}

		sourcefile >> data.temp[count1];

		sourcefile >> reader;
	}
}


-------------------------------------------------------------------------------------------

I didn't define the rest of my functions, do you think that could be causing the problem?

Does it matter if I never call them?
Last edited on
No it doesn't. And I imagine the problem is because the file is not found in the current directory. If you are using MSVC++, this is the solution directory, not the directory with the .exe in it.
what are you trying to do in getfilename() ?
Changed the program, same result, but simpler

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <fstream>
#include <iostream>



class pirate {
private:
	int temp[20];
	char city[20][25]; // oh, there are 20 sets of data and the name is a maximum of 25 chars long
	int hightemp;
	int lowtemp;
	float average;
	char highname[25];
	char lowname[25];

public:
	 //constructor. Need to initialize all numbers & chars to zero.
	void fileread(char fname[25]);
	
	void hsearch( );
	void lsearch( );
	void asearch( );
	void printdata( );
	
};

using namespace std;

pirate data;

int main()
{

	char fname[15];

	fname[0] = 'a';
	fname[1] = '8';
	fname[2] = '.';
	fname[3] = 't';
	fname[4] = 'x';
	fname[5] = 't';


	data.fileread(fname);

	return 0;
}


void pirate::fileread(char fname[15])
{
	
	ifstream sourcefile;

	sourcefile.open(fname);

	if(!sourcefile)
	{
		cout << "imput failure" << endl;
		return;
	}

	int count = 0;

	char reader;

	int count1 = 0; // keeps track of which data set
	int count2 = 0; // count of the loop to find the name.
	

	sourcefile >> reader;

	
	data.city[count1][count2] = reader;
	

	while(sourcefile)
	{
		count2++;
		while(reader != ' ')
		{
			sourcefile >> data.city[count1][count2];
			count2++;
		}

		sourcefile >> data.temp[count1];

		sourcefile >> reader;
	}
}


I checked, a8.txt is in the right directory.
Last edited on
I got it to work by calling the function by saying

fileread("a8.txt")

I didn't think that would work, but it did.

I don't really get it, though. Why does passing the name as "name" to a function expecting a char array work, when defining an array and assigning a value to each component then passing that array does not work?
Is your character array null-terminated?
Topic archived. No new replies allowed.