fwrite and fread

This is a discussion on fwrite and fread within the C Programming forums, part of the General Programming Boards category; i have a problem with writeing data to a file and reading that file after i save data to file ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    fwrite and fread

    i have a problem with writeing data to a file and reading that file
    after i save data to file i cant read that data (structure rec store[100]

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <dos.h>
    int input();
    void del();
    void list();
    int update();
    void exit();
    void menu();
    
    FILE *fp;
    
    struct rec {
           int record;
           char toolname[50];
           char cost[10];
           int quantity;
    
    };
    struct rec store[100];
    
    void choice(char ch) {
         switch(ch) {
    	  case '1':
    		input();
    		break;
    	  case '4':
    		update();
    		break;
    	  case '5':
    		exit(0);
    		break;
        }
    
    }
    
    void menu() {
         char ch;
         clrscr();
         printf("          - Menu-\n");
         printf("------------------------------\n");
         printf("-[1].Input new tools         -\n");
         printf("-[2].List the existing tools -\n");
         printf("-[3].Delete a record         -\n");
         printf("-[4].Update a record         -\n");
         printf("-[5].Exit                    -\n");
         printf("------------------------------\n");
         printf("Command:\>");
         scanf("%c",&ch);
         choice(ch);
         }
    
    int input() {
         int _record,quantity;
         char toolname[50],cost[10];
    
         clrscr();
         printf("Input new tool\n-------------------\n");
         printf("Enter record number:\n");
         scanf("%d",&_record);
         store[_record-1].record=_record;
         printf("Enter tool name:\n");
         scanf("%s",toolname);
         strcpy(store[_record-1].toolname,toolname);
         printf("Enter quantity:\n");
         scanf("%d",&quantity);
         store[_record-1].quantity=quantity;
         printf("Enter cost:\n");
         scanf("%d",&cost);
         strcpy(store[_record-1].cost,cost);
    
         //writes to file hardware.dat
         if((fp=fopen("c:\hardware","r+b"))==NULL) {
    	 printf("Cannot open file");
    	 delay(100);
    	 return 0;
    	 }
         else {
         fseek(fp,(_record-1)*sizeof(store),SEEK_SET);
         fwrite(&store,sizeof(store),1,fp);
         printf("Saved to file");
         delay(600);
         }
         fclose(fp);
         return 0;
    }
    
    int update() {
         int record;
         printf("Enter record number:");
         scanf("%d",&record);
         if((fp=fopen("c:\hardware.txt","r+b"))==NULL) {
    	 printf("Cannot open file");
    	 delay(100);
    	 return 0;
    	 }
         else {
         // fseek(fp,(record-1)*sizeof(store),SEEK_SET);
         fread(&store[record-1],sizeof(store),1,fp);
         printf("\ntool name:%s\n",store[record-1].toolname);
         printf("quantity:%d\n",store[record-1].quantity);
         printf("cost:%d\n",store[record-1].cost);
         getch();
         }
         fclose(fp);
         return 0;
    }
    
    
    
    void main() {
        clrscr();
        while(1) menu();
        }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    fread(&store[record-1],sizeof(store),1,fp);
    fwrite(&store,sizeof(store),1,fp);
    I think that the problem lies here!

    Try this :
    Code:
    fread(store[record-1], sizeof(store), 1, fp);
    fwrite(store, sizeof(store), 1, fp);
    'store' is a pointer to the first element of the array, not '&store'.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    that doesnt work

    i tried this but it works same.pls help me i have to finish this but i cant find what i do wrong

  4. #4
    Unregistered
    Guest

    Smile Here's I think your problem

    Hey!
    Do you know that after using fwrite , one has to flush the file buffer i.e,
    use >>>>>> fflush(file pointer);
    Otherwise your data is not written to file.
    Remember it.
    nadeem_bashir@rediffmail.com

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    File Buffer is automatically flushed when you call 'fclose()'

    Code:
    fseek(fp, (_record - 1) * sizeof(store[0]), SEEK_SET);

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >int input() {
    >.
    >.
    >     scanf("%d",&cost);
    Should be:
         scanf("%s",cost);
    >.
    >.
    >     if((fp=fopen("c:\hardware","r+b"))==NULL) {
    Should be:
         if((fp=fopen("c:\\hardware.txt","w+b"))==NULL) {
    >.
    >.
    >int update() {
    >.
    >.
    >     if((fp=fopen("c:\hardware.txt","r+b"))==NULL) {
    Should be:
         if((fp=fopen("c:\\hardware.txt","r+b"))==NULL) {
    >.
    >.
    >     printf("cost:%d\n",store[record-1].cost);
    Should be:
         printf("cost:%s\n",store[record-1].cost);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. Replies: 2
    Last Post: 12-26-2008, 04:38 PM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21