Thread: Poblem reading struct in a file

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    6

    Poblem reading struct in a file

    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.

    Code:
    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 my function
    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 */
    
    return 0;
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    16
    read it in a line at a time, and every six lines switch structs..

    so have a loop inside a loop, where the first loop just keeps going until there is no more data, and the inside loop reads in six lines of text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM