-1

I have given the file from which it should read. And as it encounters * symbol it should update with the next member of the structure.The input file is provided here below.

*1234567890*2223334445*santoshkumar.c.*5/13,bangalore,karnataka*
0987654321*6665554447*nirmal*13/5,bangalore,karnataka*




 #define N 5
   struct data{
         char userid[10];
         char cardid[10];
         char name[30];
         char address[100];
        };
int main(){
        struct data input[N];
        FILE *fp;
        fp=fopen("input.txt","r");
        if(fp==NULL)
            {
                printf("file open failed \n");
                return -1;
            }
       for(i=0;i<N;i++){
     fscanf("%s*%s*%s*%s*%s",&input.userid[i],&input.cardid[i],&input.name[i],&input.address[i]);
 }
 for(i=0;i<N;i++){
 printf("%s%s%s%s%s",input.userid[i],input.cardid[i],input.name[i],input.address[i]);
 }
 fclose(fp);



}
  • 4
    It doesn't really look like you've made any attempt, just opened the file. What problem are you having? – Mike Apr 24 '14 at 05:05
  • 1
    read [this](http://stackoverflow.com/questions/23191799/read-data-from-file-into-structure/23192061#23192061) and try to code for your problem – LearningC Apr 24 '14 at 05:10
  • (1)`1234567890` needs store size +1 (Eg.`char userid[11];`) (2) `fscanf("%s*%s*%s*%s*%s"` --> `fscanf(fp, "%s*%s*%s*%s*%s"` (3) scanf "%s*" --> "%[^*]*" – BLUEPIXY Apr 24 '14 at 10:01

2 Answers2

1

Change your declaration

struct data{
     char userid[11];  // +1 for NULL
     char cardid[11];
     char name[31];
     char address[101];
    };

struct data input[5]; //not record. below you used input.

and try like this

  fscanf(fp, "%s*%s*%s*%s*%s",&input[i].userid, &input[i].cardid, &input[i].name, &input[i].address);

In fscanf 1st arguments is file pointer i.e. from which file you want to read.

gangadhars
  • 2,584
  • 7
  • 41
  • 68
  • actually the data which i provided is the record of customers where i have separated each fields with Astrix. So all these datas are need to be read from the file and get updated automatically in the structure. so after that if i search giving the details of user id i should get back the complete details of the customer. this is what i need. – user3567253 Apr 24 '14 at 05:54
  • I understand your question. I'm talking about your code. – gangadhars Apr 24 '14 at 05:59
  • i have done the program to read the datas from the file but it is showing error as .userid not a structure or member. more over i don't know how to put that Astrix logic in this – user3567253 Apr 24 '14 at 07:02
  • eg.c:23: error: request for member ‘userid’ in something not a structure or union eg.c:23: error: request for member ‘cardid’ in something not a structure or union eg.c:23: error: request for member ‘name’ in something not a structure or union eg.c:23: error: request for member ‘address’ in something not a structure or union eg.c:23: warning: passing argument 1 of ‘fscanf’ from incompatible pointer type eg.c:26: error: request for member ‘name’ in something not a sn these errors i am getting – user3567253 Apr 24 '14 at 08:58
  • are you sure about input.userid[i]? Not input[i].userid? – zoska Apr 24 '14 at 09:16
  • @zoska sorry, i directly copied and edited properly. :( – gangadhars Apr 24 '14 at 09:21
  • @user3567253 now check out – gangadhars Apr 24 '14 at 09:23
  • eg.c:23: warning: passing argument 1 of ‘fscanf’ from incompatible pointer type /usr/include/stdio.h:425: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char *’ eg.c:23: warning: passing argument 2 of ‘fscanf’ from incompatible pointer type /usr/include/stdio.h:425: note: expected ‘const char * __restrict__’ but argument is of type ‘char (*)[10]’ segmentation fault – user3567253 Apr 24 '14 at 10:15
0

sample

#include <stdio.h>

#define N 5

struct data{
    char userid[10+1];
    char cardid[10+1];
    char name[30];
    char address[100];
};

int main(){
    char *dummy = "0987654321*6665554447*nirmal*13/5,bangalore,karnataka*";
    struct data input[N];
    int i=0;

    sscanf(dummy, " %10[^*]*%10[^*]*%29[^*]*%99[^*]*", input[i].userid, input[i].cardid, input[i].name, input[i].address);
    printf("%s %s %s %s\n",input[i].userid, input[i].cardid, input[i].name, input[i].address);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70