c style strings and fstream

Hello! so for a project i am working on i have to ask users for the name of a txt file they wish to pull information from (a list of names) and pull information from said txt file into a 2d array. The thing is there can be not string, we can only use iostream and fstream. I have some code below but honestly, I'm not sure how to get user input and use that for the name of the text file. I know how to get user input for a c style string but how do i use the input for a file name?


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
#include <iostream>
#include <fstream>

using namespace std;

void read_in();
int main()
{
read_in();


	return 0;
}

void read_in()
{
char filename[10];
for (int i = 0; i < 10; i++)
	{
		cout << "What is the name of your file?" << endl;
		cin.getline(filename, 10);
	}
ifstream inFile;
inFile.open(filename.c_str());
}
closed account (E0p9LyTq)
Change line 24 (inFile.open(filename.c_str()); to inFile.open(filename);.

Get rid of the for loop no matter what. It is not needed. You are asking 10 times for a file name.

I'm somewhat surprised you didn't get a compiler error with line 24 as written. I get:

24	25	D:\Programming\Projects\Test\main-cpp.cpp	[Error] request for member 'c_str' in 'filename', which is of non-class type 'char [10]'
1
2
3
4
5
for (int i = 0; i < 10; i++)
	{
		cout << "What is the name of your file?" << endl;
		cin.getline(filename, 10);
	}

This will ask the user to input the filename 10 times.

char filename[10];
What happens if the filename is more than 9 characters (1 character for null terminator) ?
Also, avoid magic numbers and remember to initialise your variables. Instead used name constants, like so
1
2
3
4
5
6
7
const int filename_len = ...
// zero initialise char array; choose whichever way you prefer
char filename[filename_len]{};
// OR
char filename[filename_len] = {};
// OR
char filename[filename_len] = { 0 };


inFile.open(filename.c_str());
c_str() is a member function of std::string not a char array. A char array is also known as a C-style string, so all you have to do is pass it as it is.
Thank you guys! i think I have managed to clear my code up a little now. I mistakenly though that the character array would need to be filled one character at a time thats why i used the for loop but now i see what you are saying. how does it look?

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
#include <iostream>
#include <fstream>

using namespace std;

void read_in();


int main()
{
read_in();





	return 0;
}

void read_in()

{
const int filnmlth = 50;
char filename[filnmlth];
		cout << "What is the name of your file? (make sure to include the txt extension)" << endl;
		cin.getline(filename, 50);
ifstream inFile;
inFile.open(filename);
}
hm so my work has gone deeper now ad i wish to output the data that i pull in from fstream. for the most part i have it figured out but i an encountering problems when i output my data it comes in the form of
1
2
3
4
5
6
7
8
9
10
11
12
BlackPantherIronManCaptainMarvelHulkDeadPoolNickFurySpidermanVisionReedRicharU
ntherIronManCaptainMarvelHulkDeadPoolNickFurySpidermanVisionReedRicharU
onManCaptainMarvelHulkDeadPoolNickFurySpidermanVisionReedRicharU
ptainMarvelHulkDeadPoolNickFurySpidermanVisionReedRicharU
rvelHulkDeadPoolNickFurySpidermanVisionReedRicharU
kDeadPoolNickFurySpidermanVisionReedRicharU
olNickFurySpidermanVisionReedRicharU
urySpidermanVisionReedRicharU
ermanVisionReedRicharU
sionReedRicharU
dRicharU

and what i would like it to look like is
1
2
3
4
5
6
7
8
9
10
BlackPanther
IronMan
CaptainMarvel
Hulk
DeadPool
NickFury
Spiderman
Vision
ReedRichards
Antman

I think it has something to do with the way i am outputting my data (can someone double check my void print_out file? line 43) but i did this way before and it worked (that time the data for my char array was declared in my code however, not pulled from an external source) may I please have some Help?

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
#include <iostream>
#include <fstream>

using namespace std;

const int filnmlth = 50;
const int one = 11;
const int two = 7;

void read_in(char (&tarray)[one][two]);
void print_out(char (&carray)[one][two]);


int main()
{
char array[one][two] = {};
read_in(array);
print_out(array);




	return 0;
}

void read_in(char (&tarray)[one][two])

{
char filename[filnmlth] = {};
		cout << "What is the name of your file? (make sure to include the .txt extension)" << endl;
		cin.getline(filename, 50);
ifstream inFile;
inFile.open(filename);
	for (int i = 0; i < one; i++)
	{
		for (int j = 0; j < two; j++)
		{
		     inFile >> tarray[i][j];
		}
	}
}

void print_out(char (&carray)[one][two])
{
	for (int i = 0; i < one; i++)
	{	
		cout << carray[i] << endl;	
	}
}


1
2
void read_in(char (&tarray)[one][two]);
void print_out(char (&carray)[one][two]);

You're really over-complicating things. You don't need to use 2D arrays.

Have a look at the example provided here: http://www.cplusplus.com/reference/istream/istream/read/
Topic archived. No new replies allowed.