1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to read strings and numbers from a file.

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

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    hi there experts,
    i am unable to read from a file using this function. this program is part of a library management project in C so here is the update books function.

    Code:
    
    void books_update(struct books book[1000],struct book_size n)
    {
     int ctr=0;  
     int asg = (n.total_books); 
    
     FILE *in;  
       
     in = fopen("INPUT.txt","r");
    
    if(in == NULL)
     return;
    
     while( fscanf(in,"%s%s%s%c%d\n",&(book[asg].subject) ,&(book[asg].title) ,&(book[asg].author) ,&(book[asg].type) ,&(book[asg].available) ) == 5 ) 
     {
      asg++;
      ctr++;
     printf("i am reading\n");
     }                    
      n.total_books = asg;
     
     printf("\t\t\nall ARTICLES Updated Successfully...\n");
     printf("--> Number of Articles added in this update %d.\n",ctr);
     printf("--> Total No. of Articles in the library %d.\n\n",(n.total_books));
    
    
    
    e.g format of book/cd in INPUT.txt
    mystery sherlock_holmes conan_doyle b 1
    info c_for_dummies unknown c 1
     
  2. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    What error are you getting while reading the file through this function??
     
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    sorry but i get no error. its just not reading in the values. when i print or use the structure's strings i get (null). so how?
    please help thanks............

    after scanning still the print says --> Number of Articles added in this update 0
    they always give 0.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What is the value of asg? (Suggestion: put a printf in that displays the *actual* value. Do not answer this with what you *think* asg is.)

    By the way your function doesn't fclose(in). It should, otherwise you will leak handles and eventually will not be able to open any files.
     

Share This Page