Thread: Need to write data to file, not sure how

  1. #1
    Unregistered
    Guest

    Question Need to write data to file, not sure how

    Here is my code, i need to add data to the file created at the top in the struct provided, and i am not sure how to do it, any help would be greatly appreciated.



    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<conio.h>

    main()
    {
    struct book
    {
    int code;
    int pages;
    char title[20];
    char author[20];
    struct book *next;
    };


    int pick;
    FILE*library;
    library=fopen("library.dat","wb");
    fread(library,sizeof(struct book),1,library);
    fclose(library);
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf ("\tPlease make a selection...\n\n");
    printf ("\t1.) Enter a book in the inventory\n");
    printf ("\t2.) Delete a book from the inventory\n");
    printf ("\t3.) Search the inventory for a book\n");
    printf ("\t4.) Display the book inventory\n");
    printf ("\t5.) Exit the program\n\n");
    printf ("\tEnter your selection: \n");
    scanf ("%d", &pick);
    fflush(stdin);
    while (pick==1)
    {
    struct book
    {
    int code;
    int pages;
    char title[20];
    char author[20];
    struct book *next;
    };
    int code;
    int pages;
    char title[20];
    char author[20];

    struct book *head,
    *work,
    *next; char answer;
    work=head=(struct book*)malloc(sizeof(struct book));
    do
    {
    printf("\nEnter Book Code: ");
    scanf("%d",&code);
    printf("\nEnter Book Author:");
    scanf("%s",&author);
    printf("\nEnter Book Title: ");
    scanf("%s",&title);
    printf("\nEnter Book number of Pages:");
    scanf("%d",&pages);
    printf("Do you wish to enter another book?:");
    if(answer=='n')
    {
    work->next=NULL;

    }
    else
    {
    work->next=(struct book*)malloc(sizeof(struct book));
    work=work->next;
    }

    return 0;


    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you want to write to the file then you'll need to use a function that does this, fwrite for example. Here's some quickie code that you can modify to get the results you need:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Record
    {
      char name[80];
    } static sRec[3];
    
    int main ( void )
    {
      FILE *fp;
      /* Input data into the struct here */
      if ( ( fp = fopen ( "tst.txt", "wb" ) ) != NULL ) {
        fwrite ( sRec, sizeof ( struct Record ), (size_t)3, fp );
        fclose ( fp );
      }
      /* Test the write by reading it back from the file */
      if ( ( fp = fopen ( "tst.txt", "rb" ) ) != NULL ) {
        fread ( sRec, sizeof ( struct Record ), (size_t)1, fp );
        fclose ( fp );
      }
      return EXIT_SUCCESS;
    }
    >fflush(stdin);
    Why does everyone do this? The behavior is undefined, never fflush input streams.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >fflush(stdin);
    Why does everyone do this? The behavior is undefined, never fflush input streams.
    They're probably using MSVC++, or have looked at a Microsoft C implementation of fflush. fflush in Microsoft's implementation actually will flush the input stream. (Or they just have a teacher or someone who said it was correct, similar to 'void main'...)

    However, if you're like me, and I know you are, we generally try to avoid undefined behaviour...

    That being said, one of the BEST books on C I have ever seen is the "Microsoft C Bible " for I think Microsoft C v5 ... before MSVC++ came out. This has THE BEST descriptions, examples, and usage of C functions I have ever seen.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Or they just have a teacher or someone who said it was correct, similar to 'void main'...
    Many times I've been tempted to start teaching programming for just this reason. Most instructors that I've seen are pretty lame.

    >That being said, one of the BEST books on C I have ever seen is the "Microsoft C Bible "
    That's one of the few books on C that I don't own yet. I'll have to check it out.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Prelude, why (size_t)3 and (size_t)1 ??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They're just typecasting to avoid warnings when they compile / run through LINT.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude, why (size_t)3 and (size_t)1 ??
    Lint good, warnings bad

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I wanna hava look at the book, but i couldn't find a sample chapter on the web. got a link?

    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 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