1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

READ different datatypes from file.

Discussion in 'C' started by jose_peeterson, Jul 29, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Hi experts,
    my following program is SUPPOSED TO read in following contents from INPUT.txt file

    Code:
    mystery sherlock_holmes conan_doyle b 0 1
    jokes what_a_gag jose_johnson b 0 1
    magazines expat alan b 0 1
    comics archies archies b 0 1
    comedy tom_and_jerry c 0 1
    
    my upload.c program
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    
    
    struct books
    {
     char title[100];
     char author[50];
     char subject[50];
     char type;
     int borrower; // 0 no one borrowed
     int available;
    
    };
    
    struct size
    {
     int total_books;
     int t_m;
    
    };
    
    void upload(struct books book[1000] ,struct size n);
    
    int main()
    {
     struct size n;
     struct size book[1000];
     
     upload(book,n);
    
    
    return 0;
    }
    
    
    void upload(struct books book[1000] ,struct size n)
    {
    
    int i=0;
    
    FILE *f;
    
    f = fopen("INPUT.txt","r");
    
     if(f == NULL)
      {
      printf("\n\tERROR : UNable to open file....\n"); 
      return;
      }
    
    while( (i < 4) && (fscanf(f,"%s%s%s%c%d%d\n",&(book[i].subject),&(book[i].title),&(book[i].author),&(book[i].type),&(book[i].borrower),&(book[i].available) ) == 6))
    {
     i++;
    printf("%s %s %s %c %d %d\n",  (book[i].subject),(book[i].title),(book[i].author),(book[i].type),(book[i].borrower),(book[i].available) );
    }
    
    fclose(f);
    
    return;
    }
    
    
    
    it does not print out the read data, so can you help me please.
    there is also a warning for upload(book,n); -- integer to pointer or something like that
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Have another look at line 2 of main().
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Also have a quick look at what you do to i between the fscanf and the printf. That might have something to do with it. You get no output by the way because the while expression immediately evaluates FALSE; this is because fscanf doesn't return 6.
     

Share This Page