I have struct
with a field of type vector of char
and I have a file .txt
with several lines in this format with names of songs.
text1
text2
text3
text4
I need to read the entire file line by line and store each line in a position of the struct
vector. I made this code here, but it stores the last line in all positions of the vector of that struct
.
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct musica2{
char *musica;
};
int main(){
FILE *arquivo;
musica2 *z = new musica2[12];
char a[255];
arquivo = fopen("teste.dat", "r");
int i=0;
if(arquivo == NULL)
cout << "Erro ao abrir arquivo." << endl;
else{
while(!feof(arquivo)){
z[i].musica = fgets(a,255,arquivo);
i++;
}
}
cout << z[3].musica;
fclose(arquivo);
return 0;
}
What can it be?