Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 24th, 2004, 6:18 PM   #1
The_Kingpin
Newbie
 
Join Date: Oct 2004
Posts: 4
Rep Power: 0 The_Kingpin is on a distinguished road
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;
}
}
The_Kingpin is offline   Reply With Quote
Old Oct 25th, 2004, 5:24 AM   #2
Daggerhex_Flynn
Programmer
 
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5 Daggerhex_Flynn is on a distinguished road
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.
Daggerhex_Flynn is offline   Reply With Quote
Old Oct 25th, 2004, 9:42 AM   #3
The_Kingpin
Newbie
 
Join Date: Oct 2004
Posts: 4
Rep Power: 0 The_Kingpin is on a distinguished road
the problem is that my array of struct "books" is of unknown size
The_Kingpin is offline   Reply With Quote
Old Oct 25th, 2004, 1:12 PM   #4
Daggerhex_Flynn
Programmer
 
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5 Daggerhex_Flynn is on a distinguished road
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.
Daggerhex_Flynn is offline   Reply With Quote
Old Oct 25th, 2004, 1:28 PM   #5
The_Kingpin
Newbie
 
Join Date: Oct 2004
Posts: 4
Rep Power: 0 The_Kingpin is on a distinguished road
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
The_Kingpin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC