Thread: Binary files

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Binary files

    Hi,

    This is the code:
    --------------------

    Code:
    #include <stdio.h>
    
    struct student{
    	int id,age;
    	char name[30];
    };
    
    
    int main()
    {
    	int i;
    	struct student record;
    	FILE *fb;
    	
    	fb = fopen("data1.txt","wb");
    	for(i=1; i<=2; i++)
    	{
    		printf("Enter the student id,name,age");
       		scanf("%d %s %d",&record.id,record.name,&record.age);
    		fwrite(&record,sizeof(struct student),1,fb);
    	}
    	fclose(fb);
    
    	fb=fopen("data1.txt","rb");
    	
    	//for(i=1; i<=sizeof(fb)+1; i++)	
                     while(!feof(fb))
    	{
    		fread(&record,sizeof(struct student),1,fb);
    		printf("ID %d\tName %s\tage %d\n",record.id,record.name,record.age);
    	}
    	
    	fclose(fb);
                    return 0;
    		
    }
    The out put of the file duplicates the last record:

    Enter the student id,name,age123 A 21
    Enter the student id,name,age456 B 22
    ID 123 Name A age 21
    ID 456 Name B age 22
    ID 456 Name B age 22

    Also, what is the wrong of the red line in the code if I replaced while(!feof(fb)) with it?
    Last edited by Moony; 06-28-2006 at 12:26 AM.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Ok, that is fine.
    But I am curious to know what is the wrong of
    Code:
    while(!feof(f))

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
             while(!feof(fb))
    	{
    		fread(&record,sizeof(struct student),1,fb);
    		printf("ID %d\tName %s\tage %d\n",record.id,record.name,record.age);
    	}
    feof() isn't true until you try reading from the file. So the above loop checks, finds you're not at the end of the file yet. You read a record but -- UH OH! -- you're at the end of the file. Now feof() would return true, but before it gets a chance to you're printing out data that you shouldn't.

    The best way to control file I/O loops is generally with the return value of whatever function you're using to read from the file. MD's example does just this and doesn't have the bug yours does.

    BTW, the sizeof operator doesn't work on file sizes. But you don't need it if you use the return value of fread() to control the loop.
    Last edited by itsme86; 06-28-2006 at 12:32 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by itsme86
    BTW, the sizeof operator doesn't work on file sizes. But you don't need it if you use the return value of fread() to control the loop.
    itsme86, can you exaplain why this happen:

    Code:
    #include <stdio.h>
    
    struct student{
    	int id,age;
    	char name[30];
    };
    
    
    int main()
    {
    	int i,id2;
    	struct student record;
    	FILE *fb;
    	
    	fb = fopen("data1.txt","wb");
    	for(i=1; i<=6; i++)	{
    		printf("Enter the student id,name,age");
       		scanf("%d %s %d",&record.id,record.name,&record.age);
    		fwrite(&record,sizeof(struct student),1,fb);
    	}
    	fclose(fb);
    
    	fb=fopen("data1.txt","rb");
    	
    	for(i=1; i<=sizeof(fb)+1; i++)	//while(fread(&record,sizeof(struct student),1,fb))
    	{
    		fread(&record,sizeof(struct student),1,fb);
    		printf("ID %d\tName %s\tage %d\n",record.id,record.name,record.age);
    	}
    	
    	fclose(fb);
    	return 0;
    		
    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Moony
    for(i=1; i<=sizeof(fb)+1; i++)
    Again, sizeof doesn't give you the file size. It gives you the size of fb, which is a pointer, and the size of a pointer is probably 4 on your system. sizeof(fb) will always give you the same number on your system no matter how big the file is that's associated with the stream fb points to.

    If you know you're writing 6 records, why not just loop 6 times again? Or use the while(fread()) method. When you try reading the 7th record fread() will return 0 and break the loop for you.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Thanks all, it is CLEAR now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 01:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM