Thread: print Random Files

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    24

    print Random Files

    hello ,
    I have a simple problem a wanaa print form Random File
    but the last printed twice???
    this is the input
    Enter the id :1

    Enter the gpa :1.2

    Enter the id :2

    Enter the gpa :2.4

    Enter the id :3

    Enter the gpa :3.5

    the size is : 3
    the id is : 1 the gpa is: 1.20
    the id is : 2 the gpa is: 2.40
    the id is : 2 the gpa is: 2.40 /// why twice?
    Press any key to continue
    the code
    Code:
    #include<stdio.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	float gpa;
    }data;
    /*********** The functions ***********/
    void read(data*);
    void print(FILE*);
    /*********** Main ***********/
    int main(){
    	data R;
    	FILE *f;
    	int i;
    	if((f=fopen("f.dat","w"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    		fseek(f,sizeof(data),SEEK_SET);
    	for(i=1;i<=3;i++){
    		read(&R);
    		fseek(f,sizeof(data)*(i-1),SEEK_SET);
    		fwrite(&R,sizeof(data),1,f);
    	}
    	}
    	print(f);
    	return 0;
    }
    
    /*********** Read ***********/
    void read(data *R){
    	printf("Enter the id :");
    	scanf("%d",&R->id);
    	printf("\n");
    	printf("Enter the gpa :");
    	scanf("%f",&R->gpa);
    	printf("\n");
    }
    
    /*********** Print ***********/
    void print(FILE *f){
    	data R;
    	int n=0,i;
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	fseek(f,sizeof(data),SEEK_END);
    	n=ftell(f)/sizeof(data);
    	printf("the size is : %d\n",n);
    	for(i=1;i<=n;i++){
    		fseek(f,sizeof(data)*(i-1),SEEK_SET);
    		fread(&R,sizeof(data),1,f);
    		printf("the id is : %d the gpa is: %.2f \n",R.id,R.gpa);
    	}
    	fclose(f);
    	}
    }
    /*********** End ***********/

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    u missed some small simple things

    1. You missed to close the file before opening the file for reading and after writing ...it is veru important of there are chances of you lossing last written data. This is very imp. in file handling.

    2. The same data was not getting printed twice as u mentioned..try intializing the variable "R" after each read then u will come to know the reason.

    3. After fseek if u do ftell the result will always be original size of file + the size you tried to fseek. Hence you need not do

    i<=n in for loop.

    The working code is as follows



    N'joy....
    Code:
    int main(){
            data R;
            FILE *f;
            int i;
            if((f=fopen("f.dat","w"))==NULL)
                    printf("sorry i can not open the file\n");
            else{
                    fseek(f,sizeof(data),SEEK_SET);
            for(i=1;i<=3;i++){
                    read(&R);
                    fseek(f,sizeof(data)*(i-1),SEEK_SET);
                    fwrite(&R,sizeof(data),1,f);
            }
            }
            fclose(f);
            print(f);
            return 0;
    }
    
    /*********** Read ***********/
    void read(data *R){
            printf("Enter the id :");
            scanf("%d",&R->id);
            printf("\n");
            printf("Enter the gpa :");
            scanf("%f",&R->gpa);
            printf("\n");
    }
    
    /*********** Print ***********/
    void print(FILE *f){
            data R;
            int n=0,i;
                    if((f=fopen("f.dat","r"))==NULL)
                    printf("sorry i can not open the file\n");
            else{
            fseek(f,sizeof(data),SEEK_END);
            n=ftell(f)/sizeof(data);
            printf("the size is : %d\n",n);
            for(i=1;i<n;i++){
                    fseek(f,sizeof(data)*(i-1),SEEK_SET);
                    fread(&R,sizeof(data),1,f);
                    printf("the id is : %d the gpa is: %.2f \n",R.id,R.gpa);
                    R.id = 0;
                    R.gpa = 0;
            }
            fclose(f);
            }
    }
    /*********** End ***********/

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: print Random Files

    First, keep the same thread for the same program, since you already have one started. You don't need to start a new thread for each question.

    In your code (reformatted for clarity):
    Code:
    /*********** Print ***********/
    void print(FILE *f)
    {
        data R;
        int n=0,i;
    
        if((f=fopen("f.dat","r"))==NULL)
        {
            printf("sorry i can not open the file\n");
        }
        else
        {
            fseek(f,sizeof(data),SEEK_END);
            n=ftell(f)/sizeof(data);
            printf("the size is : %d\n",n);
            for(i=1;i<=n;i++)
                {
                fseek(f,sizeof(data)*(i-1),SEEK_SET);
                fread(&R,sizeof(data),1,f);
                printf("the id is : %d the gpa is: %.2f \n",R.id,R.gpa);
                }
            }
            fclose(f);
        }
    }
    you are not testing for errors from either fseek() nor fread(). You are probably getting an error from fread() which is printing the data from the previous read
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    thank u cisgreat
    thank u WaltP
    i know my problem now
    thank u again

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    yup ur right waltp...actually ...i due to reformatting i missed the if else..part....anyways..thanks for that...!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Random generation of filenames and files
    By I-coNer^ in forum C Programming
    Replies: 6
    Last Post: 02-16-2005, 01:56 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 08:06 AM