Thread: C Structure and file management

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    18

    C Structure and file management

    a) Create a structure that contains information about a book, title, actor and year of publication.

    b) Write a function that saves the structure to a file, without deleting the information in the file.

    c) Write a function that reads the file and writes out the titles.

    My unfinished code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    
    int main(){
        int i;
    typedef struct     books {
    
            char title[25];
            char author[50];
            int year;
            int page;
    }books;
    books[10];
    
        /*
        FILE * post;
        post= fopen("post.txt", "w+" );
    
        while(!feof(post)){
        flose(post)
     */
    //Add book
    
    
    while (i < 10 && books.b[i].titel[0] == '\0'){
        printf("\nTitle: ");
        scanf("%s", books.b[i].title);
        printf("\nAuthor: ");
        scanf("%s", b[i].author);
        printf("\nYear: ");
        scanf("%d", &b[i].year);
        printf("\nPage: ");
        scanf("%d", &b[i].page);
    
    }
    
    
    
    
        return 0;}
    
    


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup fprintf function.

    Word of advise create a function to fill the structure from user input.
    And, create a function to output the structure to output file.

    Edit: Learn how to write the functions; it is required by your assignment.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Lookup fprintf function.

    Word of advise create a function to fill the structure from user input.
    And, create a function to output the structure to output file.

    Edit: Learn how to write the functions; it is required by your assignment.

    Tim S.
    This is not an assignment, just a problem from an old test. Ye I think I know the functions, but maybe not when working with structures. Thanks will look it up.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Lookup fprintf function.

    Word of advise create a function to fill the structure from user input.
    And, create a function to output the structure to output file.

    Edit: Learn how to write the functions; it is required by your assignment.

    Tim S.
    I got some code but it's not working 100%. The function that reads a file and saves in to a structure would be awesome if someone could get it to work.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    
    typedefstruct     books {
    
        char title[25];
        char author[50];
        int year;
        int page;
    }books;
    books b[10];
    
    
    void add(books* b){
        books tmp;
        printf("\nTitle: ");
        scanf("%s", tmp.title);
        printf("\nAuthor: ");
        scanf("%s", tmp.author);
        printf("\nYear: ");
        scanf("%d", &tmp.year);
        printf("\nPage: ");
        scanf("%d", &tmp.page);
    
        *b = tmp;
    
    }
    // Write list of books to file function
    void write(books* b){
    books tmp;
        FILE * fil;
    fil = fopen("file.txt", "r+");
        fseek(fil, 10, SEEK_SET);
    
    
    fprintf(fil,"%s \t %s \t %d \t %d", tmp.title, tmp.author, tmp.year,  tmp.page );
    fclose(fil);
    *b = tmp;
    }
    // Read list of books to file function
    void read(books* b){
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r");
        fscanf(fil, "%s \t %s \t %d \t %d",  tmp.title , tmp.author, &tmp.year, &tmp.page   );
        fclose(fil);
        *b = tmp;
    }
    int main(){
        int i = 0, val = 0;
        while(val != 5) {
            printf("\n1. Add book to list");
            printf("\n2. Write book list to command line");
            printf("\n3. Write book to file");
            printf("\n4. Read books from file\n");
            printf("5.Quit\n");
            printf("Enter 1-5: ");
        scanf("%d", &val);
    
        switch(val){
    // Add book
            case 1:
        i =0;
        while(b[i].title[0] != '\0' && i < 10){
            i++;}
        printf("(%d)", i+1);
        add(&b[i]);
            break;
    
    // Write list of books to command line
            case 2:
                for(i = 0; i <10; i++){
                    printf("\n %s \n %s \n %d \n %d \n", b[i].title, b[i].author, b[i].year, b[i].page)
                    ;}
            break;
    
    // Write list of books to file
            case 3:
                while(b[i].title[0] != '\0' && i < 10){
                    i++;}
    
                write(&b[i]);
                break;
            case 4:
                while(b[i].title[0] != '\0' && i < 10){
                    i++;}
                read(&b[i]);
    
    
    return0;
        }}}
    Last edited by Heisenberg800; 06-11-2018 at 02:40 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest not using pointer till you learn how to use them correctly.

    If you need to pass an array pass it along with the size to the function.
    In your case, it might be best to pass size and "max size" where size is the number of valid entries and "max size" is the number of possible entries in the array.

    I strongly suggest you do not lie in your comments!!

    Code:
    // Read list of books to file function
    The function only reads a single book not a list of books.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define TITLE_LENGTH 24
    #define AUTHOR_LENGTH 49
    #define LIBRARY_SIZE 10
    
    typedef struct     books
    {
        char title[TITLE_LENGTH+1];
        char author[AUTHOR_LENGTH+1];
        int year;
        int page;
    } books;
    books b[LIBRARY_SIZE];
    
    
    void add(books* b)
    {
        books tmp;
        printf("\nTitle: ");
        scanf("%s", tmp.title);
        printf("\nAuthor: ");
        scanf("%s", tmp.author);
        printf("\nYear: ");
        scanf("%d", &tmp.year);
        printf("\nPage: ");
        scanf("%d", &tmp.page);
    
        *b = tmp;
    
    }
    // Write list of books to file function
    void write(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r+");
        fseek(fil, 10, SEEK_SET);
    
    
        fprintf(fil,"%s \t %s \t %d \t %d", tmp.title, tmp.author, tmp.year,  tmp.page );
        fclose(fil);
        *b = tmp;
    }
    // Read list of books to file function
    void read(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r");
        fscanf(fil, "%s \t %s \t %d \t %d",  tmp.title, tmp.author, &tmp.year, &tmp.page   );
        fclose(fil);
        *b = tmp;
    }
    int main()
    {
        int i = 0, val = 0;
        while(val != 5)
        {
            printf("\n1. Add book to list");
            printf("\n2. Write book list to command line");
            printf("\n3. Write book to file");
            printf("\n4. Read books from file\n");
            printf("5.Quit\n");
            printf("Enter 1-5: ");
            scanf("%d", &val);
    
            switch(val)
            {
    // Add book
            case 1:
                i =0;
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
                printf("(%d)", i+1);
                add(&b[i]);
                break;
    
    // Write list of books to command line
            case 2:
                for(i = 0; i <10; i++)
                {
                    printf("\n %s \n %s \n %d \n %d \n", b[i].title, b[i].author, b[i].year, b[i].page)
                    ;
                }
                break;
    
    // Write list of books to file
            case 3:
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
    
                write(&b[i]);
                break;
            case 4:
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
                read(&b[i]);
    
    
                return 0;
            }
        }
    }
    Your code posted with defines for some of the constants and with proper formatting enclosed using code tags.

    Note: I am not going to do your homework; this site is to help you learn how to code. I suggest asking real questions; instead of vague things like my code does *not* work!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define TITLE_LENGTH 24
    #define AUTHOR_LENGTH 49
    #define LIBRARY_SIZE 10
    
    typedef struct     books
    {
        char title[TITLE_LENGTH+1];
        char author[AUTHOR_LENGTH+1];
        int year;
        int page;
    } books;
    books b[LIBRARY_SIZE];
    
    
    void add(books* b)
    {
        books tmp;
        printf("\nTitle: ");
        scanf("%s", tmp.title);
        printf("\nAuthor: ");
        scanf("%s", tmp.author);
        printf("\nYear: ");
        scanf("%d", &tmp.year);
        printf("\nPage: ");
        scanf("%d", &tmp.page);
    
        *b = tmp;
    
    }
    // Write list of books to file function
    void write(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r+");
        fseek(fil, 10, SEEK_SET);
    
    
        fprintf(fil,"%s \t %s \t %d \t %d", tmp.title, tmp.author, tmp.year,  tmp.page );
        fclose(fil);
        *b = tmp;
    }
    // Read list of books to file function
    void read(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r");
        fscanf(fil, "%s \t %s \t %d \t %d",  tmp.title, tmp.author, &tmp.year, &tmp.page   );
        fclose(fil);
        *b = tmp;
    }
    int main()
    {
        int i = 0, val = 0;
        while(val != 5)
        {
            printf("\n1. Add book to list");
            printf("\n2. Write book list to command line");
            printf("\n3. Write book to file");
            printf("\n4. Read books from file\n");
            printf("5.Quit\n");
            printf("Enter 1-5: ");
            scanf("%d", &val);
    
            switch(val)
            {
    // Add book
            case 1:
                i =0;
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
                printf("(%d)", i+1);
                add(&b[i]);
                break;
    
    // Write list of books to command line
            case 2:
                for(i = 0; i <10; i++)
                {
                    printf("\n %s \n %s \n %d \n %d \n", b[i].title, b[i].author, b[i].year, b[i].page)
                    ;
                }
                break;
    
    // Write list of books to file
            case 3:
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
    
                write(&b[i]);
                break;
            case 4:
                while(b[i].title[0] != '\0' && i < 10)
                {
                    i++;
                }
                read(&b[i]);
    
    
                return 0;
            }
        }
    }
    Your code posted with defines for some of the constants and with proper formatting enclosed using code tags.

    Note: I am not going to do your homework; this site is to help you learn how to code. I suggest asking real questions; instead of vague things like my code does *not* work!

    Tim S.
    Yeah you are right Tim. I'm just stressed cause I have test in a few hours. Will probably fail this one oh well. Do you know how one could read from files to save to structures? I don't get how we can go from a text file to different locations in a structure if you get what I mean.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    #define TITLE_LENGTH 24
    #define AUTHOR_LENGTH 49
    #define LIBRARY_SIZE 10
    
    typedef struct     books
    {
        char title[TITLE_LENGTH+1];
        char author[AUTHOR_LENGTH+1];
        int year;
        int page;
    } books;
    books b[LIBRARY_SIZE];
    
    void DisplayBook(books tmp2);
    bool ReadBookFromFile(FILE * fil, books * b);
    bool WriteBookToFile(FILE * fil, books b);
    
    void add(books* b)
    {
        books tmp;
        printf("\nTitle: ");
        scanf("%s", tmp.title);
        printf("\nAuthor: ");
        scanf("%s", tmp.author);
        printf("\nYear: ");
        scanf("%d", &tmp.year);
        printf("\nPage: ");
        scanf("%d", &tmp.page);
    
        *b = tmp;
    
    }
    
    void DisplayBook(books tmp2)
    {
        printf("\n %s \n %s \n %d \n %d \n", tmp2.title, tmp2.author, tmp2.year, tmp2.page);
    }
    
    // Write book out to file
    bool WriteBookToFile(FILE * fil, books tmp)
    {
        int chars_written = 0;
    
        chars_written = fprintf(fil,"%s \t %s \t %d \t %d", tmp.title, tmp.author, tmp.year,  tmp.page );
    
        return !!chars_written;
    }
    
    // Write list of books to file function
    void write(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r+");
        fseek(fil, 10, SEEK_SET);
    
    
        fprintf(fil,"%s \t %s \t %d \t %d", tmp.title, tmp.author, tmp.year,  tmp.page );
        fclose(fil);
        *b = tmp;
    }
    
    // Read a book from file
    bool ReadBookFromFile(FILE * fil, books * tmp)
    {
        int args_read = 0;
    
        args_read = fscanf(fil, "%s \t %s \t %d \t %d",  tmp->title, tmp->author, &(tmp->year), &(tmp->page) );
    
        return args_read == 4;
    }
    // Read list of books to file function
    void read(books* b)
    {
        books tmp;
        FILE * fil;
        fil = fopen("file.txt", "r");
        fscanf(fil, "%s \t %s \t %d \t %d",  tmp.title, tmp.author, &tmp.year, &tmp.page   );
        fclose(fil);
        *b = tmp;
    }
    int main()
    {
        FILE * filIn;
        FILE * filOut;
        books tmp = {"One", "Two", 1, 2};
        books tmp2;
    
        DisplayBook(tmp);
    
        filOut = fopen("file.txt", "w");
        if (filOut == NULL)
        {
            fprintf(stderr, "File open for write error!\n");
            exit(1);
        }
    
        WriteBookToFile(filOut, tmp);
    
        fclose(filOut);
    
        filIn = fopen("file.txt", "r");
        if (filIn == NULL)
        {
            fprintf(stderr, "File open for read error!\n");
            exit(1);
        }
    
        if (ReadBookFromFile(filIn, &tmp2))
            DisplayBook(tmp2);
    
        fclose(filIn);
    
        return 0;
    }
    This may help you to do file in/out.

    Note: I used bad parameter names to match your code!

    Edit: I was amazed at how hard C File I/O was after about an decade of not doing it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Management (Help Please)
    By İlkay İvrendi in forum C Programming
    Replies: 10
    Last Post: 05-21-2014, 07:37 AM
  2. File management, help please!
    By zetaXX in forum C Programming
    Replies: 8
    Last Post: 06-03-2013, 12:05 PM
  3. file management in c
    By Animesh Gaitond in forum C Programming
    Replies: 4
    Last Post: 09-20-2011, 10:28 AM
  4. File Management in C
    By Figen in forum C Programming
    Replies: 6
    Last Post: 01-31-2011, 03:14 PM
  5. need help for file management
    By edesign in forum C Programming
    Replies: 5
    Last Post: 02-08-2008, 11:29 PM

Tags for this Thread