Trying to fill a char array pointer with strings from file.

I am struggling with an exercise that requires you to take strings from a file, and bring them into a character pointer array, so that each element in the character array points to a string. Then if you cout << array[i], each index will print the string. I am fairly new to C++, and pointers in general so this task has been difficult.

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

using namespace std;

int main()
{
	ifstream file;
	
	file.open("input.txt");
  
	char *fileHolder[9];
        char *temp;

	for (int i = 0; i < 9; i++){
		file >> temp;
		fileHolder[i] = temp;
	}

	for (int j = 0; j < 9; j++){
		cout << fileHolder[j] << endl; // prints each string to screen
	}
    
    file.close();
    return 0;
}
Last edited on
This is all there is, input.txt is a text document with 9 words, and I want to store those 9 words into a character pointer array.
Yea, it does actually :).

Could you explain what changed when you added the
fileHolder[i] = new char[500]
and the
delete [] fileHolder[i]

I am kind of a noob still. :)
Last edited on
Cool, thank you very much.
Topic archived. No new replies allowed.