Read a file and transfer to a vector

Asked

Viewed 42 times

0

In case, what would be in the file would be like this:

Baidu 12

Whastapp 53

. . .

and so on, it would be the name of the "app", and the number q would be the size of the "app", But I can’t read a full word, and I still wanted to be able to separate the name and the size in info[]. name and info[]. tam.

What I’ve done so far is basically this, of course I’ve done some other tests, but none have shown results:

  #include <stdio.h>
typedef struct app
{
char nome;
int tam;
} dados;
void trans (dados inf[6], FILE *Apps);
int main(int argc, char** argv)
{
FILE *App = fopen("App.txt", "r");
if (App == NULL)
{
    printf("ERRO! O arquivo não foi aberto!\n");
}
dados info[6];
trans(info, App);
printf("%c", info[0].nome);
return 0;
}

void trans (dados inf[6], FILE *Apps)
{
int i = 0, j=0;
char caracter, data[4];

while((caracter= fgetc(Apps))!='\n')
{
    data[j]= caracter;
    j++;
    if(j>3)
        break;
}
inf[i].nome=data;

}

  • With "char name;" you are setting the name variable to have a single character. Study how to define a string in C (array of characters with the terminator ' 0'). To assign one string to another in C we use the strcpy function of <string. h>.

  • I ended up having some stupid mistakes, I was a while without seeing C, but I fixed the code already

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.