c - Reading multiple strings from a file using one fscanf -


so idea read file , put in fields of struct carro.

thing is, when try printf structure variable (dados[1].marca instance), doesn't show me in console.

i can't see problem is, since fscanf returns 8 (8 read variables).

the file i'm using list of cars, each line containing info specific model, , has following format:

ford[]transit custom van 270l1 econetic base 2.2tdci h1[\t]2013[\t]3[\t]2[\n] (...) 

#include <stdio.h> #include <stdlib.h> #include <string.h>  typedef struct    {    char marca[50];     char modelo[50];    char ano[5];    char lugares[5];    char portas[5];    }carro;  main()    {    file *fp=null;    carro dados[4700];    int i=0;     fp=fopen("car.txt","r");    while (fscanf(fp,"%[^ ] %[^\t] %[^\t] %[^\t] %[^\n]",           dados[i].          marca,           dados[i].modelo,           dados[i].ano,          dados[i].lugares,          dados[i].portas)!=eof);         {       i++;       }     fclose(fp);    } 

see comments in code below:

#include <stdio.h> #include <stdlib.h> #include <string.h>  typedef struct    {    char marca[50];    char modelo[50];    char ano[5];    char lugares[5];    char portas[5];    } carro;  int main()     // changed 'int' type.    {    file *fp=null;    carro dados[4700];    int i=0;    int ncnt;   // added line printing result.     fp=fopen("car.txt","r"); //should check fp here ensure file opened.      while(fscanf(fp," %[^ ] %[^\t] %[^\t] %[^\t] %[^\n]\n", // modified 'gobble-up' newlines.          dados[i].marca,          dados[i].modelo,          dados[i].ano,          dados[i].lugares,          dados[i].portas)!=eof)  // removed semicolon. per 'bluepixy'       {       i++;       }    fclose(fp);     /* added print result. */    for(ncnt = 0; ncnt < i; ++ncnt)       printf("marca[%s] modelo[%s] ano[%s] lugares[%s] portas[%s]\n",          dados[ncnt].marca,          dados[ncnt].modelo,          dados[ncnt].ano,          dados[ncnt].lugares,          dados[ncnt].portas          );     return(0);      } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -