Problem reading struct in a file

T

The_Kingpin

Hi again guys,

I've decided to cut my project in section and I found it way easier like
this. I'm having a little problem reading struct in a file though. I think
after this I'll be able to handle it on my own.

Right now the file is opened correctly and I'm able to read each line and
print them in a new file. My problem is to insert each struct (which are
made of 6 consecutive lines) as an item in my array.

Code:
for(i=0, j=0; c[i]!='\t'; i++, j++) {
/* Loops until a tab character is reached */
books[num_books].title[j] = c[i];
}

/* add a null character to terminate string */
books[num_books].title[j] = '\0';

When reading a line in the file books.txt, this is how I currently assign
a value to a field of the struct infoBook. Then, when the struct is
completed (with the 6 fields having value), I need to add this struct to
the array books, which I'm not able right now I'm not sure my structure is
correct, but I tried regrouping all the info I learned so far...

Here's the part of the function where I treat the file info
Code:
file = fopen("books.txt", "r");
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
while (!feof(file)){

if(num_books==0) {
books = calloc(1, sizeof(bookInfo));
}
else {
books = realloc(books, (num_books+1)*sizeof(bookInfo));
}

/* This is where I try to store info from the file in our struct field.
*/

//Title field
for(i=0, j=0; c[i]!='\t'; i++, j++) {
/* this keeps looping until a tab character is reached */
books[num_books].title[j] = c[i];
}
/* add a null character to terminate string */
books[num_books].title[j] = '\0';


/* Autor field */
for(i++, j=0; c[i]!='\t'; i++, j++) {
books[num_books].autor[j] = c[i];
}
books[num_books].autor = '\0';


/* Editor field */
for(i++, j=0; c[i]!='\0' && c[i]!='\n'; i++, j++) {
/* store the gender but ignore the last newline character */
books[num_books].editor[j] = c[i];
}
books[num_books].editor[j] = '\0';


num_books++; /* keep track of how many we stored */
}
 
M

Malcolm

The_Kingpin said:
for(i=0, j=0; c!='\t'; i++, j++) {
/* Loops until a tab character is reached */
books[num_books].title[j] = c;
}

/* add a null character to terminate string */
books[num_books].title[j] = '\0';
[/CODE]

Break it down into functions. If you read in a whole line with fgets(), your
input is now a string.

int parseline(BOOK *out, const char *str)
{
int i;

/* stop on tab, but also on end of string or overflow so we don't crash */
for(i=0; str != '\t' && str != 0 && i < 63;i++)
out->title = str;
out->title = 0;
/* return -1 on parse error */
if(str != '\t')
return -1;

/* for convenience, move the pointer along for the next field */
str += i +1;

/* now do the same thing for the other fields */

/* 0 to indicate OK */
return 0;
}

the main loop becomes

while(fgets(buff, sizeof(buff), fp) != NULL)
{
BOOK book;
if(parseline(&book, buff) == 0)
{
books = realloc(books, (Nbooks + 1) * sizeof(BOOK));
if(!books)
/* out of memory, bale */
books[Nbooks] = book;
Nbooks++;
}
else
/* we've been fed a bad line. Complain */
}
 
T

The_Kingpin

Thanks a lot Malcolm, I understand almost everything. However, I do not
quit understand the main loop. What is the type of the buff ? Plus I would
appreciate a lot if you could just epxlain the codeline

books = realloc(books, (Nbooks + 1) * sizeof(BOOK));

because I didnt't see this command yet. I'll try to get info on the web as
to it's function. Thanks for our help and sorry for looking so stupid, I'm
mainly trying to make the project to understand :(
 
T

The_Kingpin

Thanks a lot Malcolm, I understand almost everything. However, I do not
quit understand the main loop. What is the type of the buff ? Plus I would
appreciate a lot if you could just epxlain the codeline

books = realloc(books, (Nbooks + 1) * sizeof(BOOK));

because I didnt't see this command yet. I'll try to get info on the web as
to it's function. Thanks for our help and sorry for looking so stupid, I'm
mainly trying to make the project to understand :(
 
J

Jonathan Adams

"The_Kingpin said:
Thanks a lot Malcolm, I understand almost everything. However, I do not
quit understand the main loop. What is the type of the buff ? Plus I would
appreciate a lot if you could just epxlain the codeline

books = realloc(books, (Nbooks + 1) * sizeof(BOOK));

because I didnt't see this command yet. I'll try to get info on the web as
to it's function. Thanks for our help and sorry for looking so stupid, I'm
mainly trying to make the project to understand :(

What realloc() does is take a buffer previously returned by malloc(),
calloc(), or realloc(), and change its size, either by adjusting its
bookkeeping information, or by allocating a new buffer and copying the
old information over.

The above is generally bad style, though, since it it impossible to
recover from allocation failures. Something like:

newbooks = realloc(books, (Nbooks + 1) * sizeof(BOOK));
if (newbooks == NULL) {
/* handle errors */
}
books = newbooks;

is much safer.

Cheers,
- jonathan
 
T

The_Kingpin

I get some errors while compiling... First, the line
"BOOK book;" give some Undeclared identifier error. Is it because I can't
declare a new instance of a struct directly in a function ?

The other error I get is with the line
"books[num_books] = book;" and the error is "cannot convert from int to
bookInfo". I don't know why I got this error because we assign the values
of book to the books with the num_books value...

Thanks
 
M

Malcolm

The_Kingpin said:
I get some errors while compiling... First, the line
"BOOK book;" give some Undeclared identifier error. Is it because I can't
declare a new instance of a struct directly in a function ?
BOOK should be bookInfo

The other error I get is with the line
"books[num_books] = book;" and the error is "cannot convert from int to
bookInfo". I don't know why I got this error because we assign the values
of book to the books with the num_books value...
This error will then go away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top