Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how to read strings and numbers from a file. (http://www.go4expert.com/forums/read-strings-file-t26297/)

jose_peeterson 17Jul2011 19:44

how to read strings and numbers from a file.
 
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

poornaMoksha 18Jul2011 17:28

Re: how to read strings and numbers from a file.
 
What error are you getting while reading the file through this function??

jose_peeterson 18Jul2011 19:08

Re: how to read strings and numbers from a file.
 
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.

xpi0t0s 19Jul2011 12:21

Re: how to read strings and numbers from a file.
 
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.


All times are GMT +5.5. The time now is 13:16.