how to read strings and numbers from a file.

jose_peeterson's Avatar, Join Date: May 2011
Contributor
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
0
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
What error are you getting while reading the file through this function??
0
jose_peeterson's Avatar, Join Date: May 2011
Contributor
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.