Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15
  1. #11

    Default Re: Help on C File Handling


    Your write routine has the problem. You need to fseek every write.
    Code:
    		printf("\nEnter no. ");
    		scanf("%d",&x);
    		printf("\nEnter name: ");
    		scanf("%s",&st);
    		strcpy(my_data[x-1].name,st);
    		printf("\nOwes? ");
    		scanf("%f",&owe);
    		my_data[x-1].owes=owe;
    		printf("\nWRITER");
    		
    		/* randomly move pointer to the right record position to write */
    		fseek(file_pointer, (x-1) * sizeof(struct entry), SEEK_SET );
    		fwrite(&my_data[x-1],sizeof(struct entry),1,file_pointer);
    Your original read routine works also with this modification on the write routine.

  2. #12

    Default Re: Help on C File Handling

    I wrote your code.
    It still doesn't work.

  3. #13

    Default Re: Help on C File Handling

    Delete writer.dat and test again. I recompiled the code with my modification and works OK.
    I use gcc, not turbo C, but this does not matter anyway for both supports ANSI.

    Be sure also to input 5 records and nos. 1,2,3,4,5 complete. And put back your original read routine (w/o fseek).
    Code:
    #include <stdio.h>
    #include <string.h>
    #define INDEX 5
    
    #define getch getchar
    #define clrscr()
    
    struct entry{
        char name[20];
        float owes;
        }my_data[INDEX];
    
    main()
    {
    char letra;
    int x;
    for(x=0;x<INDEX;x++)
        {
        my_data[x].owes=0;
        }
    do
    {
    clrscr();
    printf("\nW-rite  R-ead  Q-uit");
    letra=getch();
    switch(toupper(letra))
        {
        case('W'):
            writer();
            break;
        case('R'):
            reader();
            break;
        case('Q'):
            break;
        }
    }while((toupper(letra))!='Q');
    }
    
    writer()
    {
    int x;
    float owe;
    char st[20];
        FILE *file_pointer;
        if((file_pointer=fopen("writer.dat","a"))!=NULL)
            {
            printf("\nEnter no. ");
            scanf("%d",&x);
            printf("\nEnter name: ");
            scanf("%s",&st);
            strcpy(my_data[x-1].name,st);
            printf("\nOwes? ");
            scanf("%f",&owe);
            my_data[x-1].owes=owe;
            printf("\nWRITER");
    
            /* randomly move pointer to the right record position to write */
            fseek(file_pointer, (x-1) * sizeof(struct entry), SEEK_SET );
            fwrite(&my_data[x-1],sizeof(struct entry),1,file_pointer);
    
            }
        else printf("Error writing writer.dat\n");
        fclose(file_pointer);
        getch();
    }
    
    reader()
    {
        FILE *file_pointer;
        struct entry buffer[INDEX];
        int x;
        if((file_pointer=fopen("writer.dat","r"))==NULL)
            {
            printf("Error reading writer.dat\n");
            }
        else
            {
            printf("\nOwes: ");
            rewind(file_pointer);
            fread(my_data,sizeof(struct entry),INDEX,file_pointer);
            for(x=0;x<INDEX;x++)
                {
                printf("\nRecord %d %3.2f",x,my_data[x].owes);
                }
            fclose(file_pointer);
            }
        getch();
        return(0);
    }

  4. #14

    Default Re: Help on C File Handling

    YES!!! IT WORKS!!! IT WORKS!!!
    Thank you very much.
    Now, I have a sample for this type of problem. I really had a hard time looking for a demonstration of the fread, fwrite, and fseek function. Thank you so much.
    I'll just post again if ever I have a question. Thank you very much for helping me.

  5. #15

    Default Re: Help on C File Handling

    You are welcome!
    Glad to hear that it worked.

  6.    Advertisement

Page 2 of 2 FirstFirst 12

Similar Threads

 
  1. Need help on compressing video file size
    By warblood360 in forum Apple Devices
    Replies: 1
    Last Post: 07-05-2013, 01:17 AM
  2. pls help on encrypted back up files
    By gabs in forum Computer Hardware
    Replies: 31
    Last Post: 08-07-2010, 01:31 PM
  3. Need help on old WordStar files
    By archnacorda in forum Computer Hardware
    Replies: 0
    Last Post: 07-31-2010, 11:00 AM
  4. help on videocams
    By Bao in forum Gizmos & Gadgets (Old)
    Replies: 27
    Last Post: 03-29-2008, 03:56 PM
  5. Guys need help on Mac OS X and Win XP using Flash drive file Transfer
    By ryan_boy22 in forum Software & Games (Old)
    Replies: 11
    Last Post: 12-26-2007, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top