I am trying to read in rows from a file and save them in to a char* data[] variable with the code that follows:
fstream level;
level.open("level.txt");
char* row;
int i = 0;
char* data[3];
while (level >> row) {
data[i] = row;
cout << i << " " << data[i] << "\n";
i++;
}
for (i = 0; i < 3; i++) {
cout << "\n " << i << " = " << data[i];
}
level.close();
The contents of the file are as follows:
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
and the first cout outputs the following, as you'd expect:
0 aaaaaaaaaaaaaaaaaaaaaaaa
1 aaaaaaaaaaaaaaaaaaaaaaaa
2 aaaaaaaaaaaaaaaaaaaaaaaa
but then the second one outputs the following:
0 =
1 =
2 =
as If all the data from data[] has been erased. Can anyone advise me as to why this is happening?
thanks for your help.