Thread: Binary files

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Binary files

    i'm doing an assignment with binary files and link lists.
    the following bit of code kinda works but it repeats the last entry from the file. I can't understand why. Any suggestions wud be greatly appreciated
    Code:
     void readfromfile()
    {
    struct Copy{
    	 int       ccode;
        char      cdescription[50];
        char      clicense;
        char      cspec[50];
        int       cunits;
        int       creorder;
        float 	  ccost;
    };
    struct Copy copy;
    FILE *frPtr;
    
    
    if ((frPtr = fopen("software", "rb"))==NULL){
      printf("Cannot open file \n");
     }
     else
     {
     	fseek (frPtr,0,SEEK_SET);
    	while (!feof(frPtr))
       {
    		 fread (&copy,sizeof(struct Copy),1,frPtr);
           printf("%d\n\n", copy.ccode);
           printf("%s\n", copy.cdescription);
           printf("%c\n\n", copy.clicense);
           printf("%s\n", copy.cspec);
           printf("%d\n\n", copy.cunits);
           printf("%d\n\n", copy.creorder);
           printf("%f\n\n", copy.ccost);
           system("pause");
    	}
      }
      fclose(frPtr);
    
    printf("To read data from a file if the file exists and load it into the Linked List.\n");
    system("pause");
    
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by docetes
    the following bit of code kinda works but it repeats the last entry from the file. I can't understand why. Any suggestions wud be greatly appreciated
    Don't use feof for loop control.
    To correct the problem, always follow this rule: use the return code from the read function to determine when you've hit EOF.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    cheers. You just saved me hours of trawling through ****

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