1
I have a struct
with a vector type field of char
and I have a file .txt
with several lines in this format with song names.
text 1
text2
text 3
text4
I need to read the entire file row by row and store each row in a vector position of struct
. I made this code here, but it stores the last line in all the positions of this vector 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?