![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 4
Rep Power: 0
![]() |
Hi all, newb here ! I'm working on a project and having some problem. Once I can get rid of this prob I'll be in business !
I need to open a file named books.txt which contains a number of line we don't know. There's one information per line and each group of 6 lines forms a struct. 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.
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';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 complete code #include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
struct book
{
char title[80];
char autor[40];
char editor[20];
char ISBN[10];
char subject[20];
int release;
};
typedef struct book bookInfo;
int main()
{
char x[50];
FILE *file; //file opened
infoLivre *books; // array of struct of undefined length
int num_books = 0;
int i, j;
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(infoLivre));
}
else {
books = realloc(books, (num_books+1)*sizeof(bookInfo));
}
/* now try to store relevant info in our struct field.
can do it character at a time as sscanf won't work as
it falls apart with spaces between names */
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';
for(i++, j=0; c[i]!='\t'; i++, j++) {
books[num_books].autor[j] = c[i];
}
books[num_books].autor = '\0';
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 */
}
fclose(file); /* close file */
/* print all the info */
for(i=0; i<num_books; i++) {
printf("%s\t ", books[i].title);
printf("%s\t ", books[i].autor);
printf("%d\t\n", books[i].editor);
}
if(books!=NULL) {
free(books); /* free any allocated memory */
}
return 0;
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5
![]() |
Make books a fixed size array of pointers to type "struct book" aka "bookInfo".
bookInfo *books[MAXSIZE]; Than assign each books pointer to a book. while(i++ < MAXSIZE || (fgets(buffer, 255,fptr) != NULL ) { books[ i ] =new_book;} OR There are many other ways to solve the problem as well. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 4
Rep Power: 0
![]() |
the problem is that my array of struct "books" is of unknown size
![]() |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5
![]() |
Than you should use a linked list.
An array is a tool of static allocation. You could make it huge and just don't go beyond it. or else use a tool of dynamic allocation, a basic linked list or a basic tree. If you want me to do your program using a basic linked list approach than post part of the datafile, and I'll do it tomorrow. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2004
Posts: 4
Rep Power: 0
![]() |
Thanks dagger, I appreciate your advices. I'll try to do it by myself with the informations I can find around here (haven't had classes on linked lists yet).
If it doesn't work or I can't find enough informations, I'll try doing what you said first, using a pre-determined array with a max size of 1000. Thanks for the support, I appreciate a lot. Frank |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|