Problem with Binary Files I/O (fread function)

This is a discussion on Problem with Binary Files I/O (fread function) within the C Programming forums, part of the General Programming Boards category; But if you ALWAYS want to seek to 0, then you don't really need to involve i or sizeof(account), right? ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But if you ALWAYS want to seek to 0, then you don't really need to involve i or sizeof(account), right?

    You only set i = 0 in the beginning of the program, so if you go back to show users again, it would be incorrect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by matsp View Post
    You only set i = 0 in the beginning of the program, so if you go back to show users again, it would be incorrect.
    I'm sorry but i didn't really get this one. Here's another loop i implemented but i'm having the same problem.I'm convinced that the program fails at going back to the start of the file. i did change fseek but nothing happened. Here's the code i'm having so far(i have other functions but they are working fine and not related to the problem):
    Code:
    #include <stdio.h>
    #include <string.h>
    #define len 20
    
    	
    	
    
    	typedef struct {
    		char name[len];
    		char password[len];
    		
    	}account;
    	
    
    int 
    main()
    {
    	FILE *inp,*inp2;
    	account ptr,ptr2;
    	account arr[len];
    	int menu_choice=1, i=0,j=1, test, accounts_num;
    	
    	
    	
          if ( ( inp = fopen( "accounts.bin", "rb+" ) ) == NULL ) {
           		printf( "File Doesn't exist. A file was created...\n\n\n" );
           		fopen("accounts.bin", "wb+");
           	
           		return(0);
          }
          while(menu_choice!=6){
          printf("\nChoose one of these operations: ");
          printf("\n1- View Account\n");
          printf("2- Add new Account\n");
          scanf("&#37;d",&menu_choice);
          
          if (menu_choice==2){
          		
          		printf("Please Enter Username: ");
    			scanf("%s",ptr.name);
    			printf("Please Enter Password: ");
    			scanf("%s",ptr.password);
     		
    		 	inp=fopen("accounts.bin", "wb+");
    		 	fwrite(&ptr,sizeof(account),1,inp);		
    			fflush(inp);  
          }
          
          if (menu_choice==1){
       			rewind(inp); //just trying
          		
     	     	fseek(inp,0,SEEK_SET);
    					
    			  		while(fread(&arr[i],sizeof (account),1,inp)==1){ 
    			  		
    		      			    	printf("TEST: %d- %s\n",i+1,arr[i].name);
    		      			    		i++;
    		      						accounts_num++;
      					}
          			
          			
          		
          }
       
          
     
    }
     
     fclose(inp);
    return(0);
    }
    i know i'm not supposed to say this but this is really getting on my nerves !!

  3. #18
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by g33koo View Post
    I'm sorry but i didn't really get this one. Here's another loop i implemented but i'm having the same problem.I'm convinced that the program fails at going back to the start of the file. i did change fseek but nothing happened. Here's the code i'm having so far(i have other functions but they are working fine and not related to the problem):
    Code:
    #include <stdio.h>
    #include <string.h>
    #define len 20
    
    	
    	
    
    	typedef struct {
    		char name[len];
    		char password[len];
    		
    	}account;
    	
    
    int 
    main()
    {
    	FILE *inp,*inp2;
    	account ptr,ptr2;
    	account arr[len];
    	int menu_choice=1, i=0,j=1, test, accounts_num;
    	
    	
    	
          if ( ( inp = fopen( "accounts.bin", "rb+" ) ) == NULL ) {
           		printf( "File Doesn't exist. A file was created...\n\n\n" );
           		fopen("accounts.bin", "wb+");
           	
           		return(0);
          }
          while(menu_choice!=6){
          printf("\nChoose one of these operations: ");
          printf("\n1- View Account\n");
          printf("2- Add new Account\n");
          scanf("%d",&menu_choice);
          
          if (menu_choice==2){
          		
          		printf("Please Enter Username: ");
    			scanf("%s",ptr.name);
    			printf("Please Enter Password: ");
    			scanf("%s",ptr.password);
     		
    		 	inp=fopen("accounts.bin", "wb+");
    		 	fwrite(&ptr,sizeof(account),1,inp);		
    			fflush(inp);  
          }
          
          if (menu_choice==1){
       			rewind(inp); //just trying
          		
     	     	fseek(inp,0,SEEK_SET);
    					
    			  		while(fread(&arr[i],sizeof (account),1,inp)==1){ 
    			  		
    		      			    	printf("TEST: %d- %s\n",i+1,arr[i].name);
    		      			    		i++;
    		      						accounts_num++;
      					}
          			
          			
          		
          }
       
          
     
    }
     
     fclose(inp);
    return(0);
    }
    i know i'm not supposed to say this but this is really getting on my nerves !!
    Here is a working version of the script:
    What you missed was simply to use ab+ instead of wb+.

    Code:
    	FILE *inp, *inp2;
    	account ptr, ptr2;
    	account arr[len];
    	int menu_choice=1, iInfLoop = 0, i = 0;
    
    	if ( ( inp = fopen( "accounts.bin", "rb+" ) ) == NULL ) 
    	{
    		printf( "File Doesn't exist. A file was created...\n\n\n" );
    		inp = fopen("accounts.bin", "wb+");
    		fclose(inp); // Should close the file after you used it.
    		return(0);
    	}
    
    	while(menu_choice!=6)
    	{
    		printf("\nChoose one of these operations: ");
    		printf("\n1- View Account\n");
    		printf("2- Add new Account\n");
    		scanf("%d",&menu_choice);
    
    		if (menu_choice==2)
    		{
    
    			printf("Please Enter Username: ");
    			scanf("%s",ptr.name);
    			printf("Please Enter Password: ");
    			scanf("%s",ptr.password);
    
    			inp=fopen("accounts.bin", "ab+"); // <---
    			fseek(inp, 0L, SEEK_END);
    			fwrite(&ptr,sizeof(account),1,inp);		
    			fclose(inp); // Should close the file after you used it.
    		}
    
    		if (menu_choice==1)
    		{
    			inp = fopen("accounts.bin", "rb+");
    
    			fseek(inp,0,SEEK_SET);
    			i = 0; // Since we are inside a loop, reset the variable before we collect the data
    
    			while(iInfLoop == 0)
    			{ 
    				if (fread(&arr[i],sizeof(account),1,inp) == 0)
    					break;
    				
    				printf("TEST: %d- %s\n",(i+1),arr[i].name);
    				i++;
    			}
    
    			fclose(inp); // Should close the file after you used it.
    		}
    
    	}
    	return(0);

  4. #19
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    From what i saw, using wb+ would first truncate the file before writing new data into it.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Piffo, whilst your code works OK as far as I can see, I'd like to make the following comments:
    1. It is not necessary to open and close the file for every operation (it may not make much difference in this case, but learning how to make sure that files are consistent WITHOUT opening and closing will be beneficial).
    2.
    Code:
    			inp=fopen("accounts.bin", "ab+"); // <---
    			fseek(inp, 0L, SEEK_END);
    If you use "a" mode, it positions the "current location" at the end of the file, so fseek is not needed.
    3.
    Code:
     			inp = fopen("accounts.bin", "rb+");
    
    			fseek(inp,0,SEEK_SET);
    Again, using fseek is not needed immediately after the file has been opened in this case. Opening a file in "r" mode.

    Whilst most OS's are optimized to handle the same file opened and closes multiple times in a good way, it is still not necessary, and an fseek() operation is probably about 100x faster than the open/close pair [that is a guess, but I'm pretty certain that there will be a noticable difference between opening and closing a file 100 times and fseeking 100 times in an already opened file].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by matsp View Post
    Piffo, whilst your code works OK as far as I can see, I'd like to make the following comments:
    1. It is not necessary to open and close the file for every operation (it may not make much difference in this case, but learning how to make sure that files are consistent WITHOUT opening and closing will be beneficial).

    --
    Mats
    I reason that, A closed file can be opened somewere else if needed. Thats why i like lock files.

  7. #22
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Piffo, thanks a lot. So i guess what i was doing was overwriting the data i entered with "wb+" while using "ab+" i'm appending them ?. However, i'm pretty sure that at some point the program did display the data without using the append mode. For the moment i'm sticking with your solution until i find another one.
    Matsp, i totally agree that closing the file everytime is not necessary. Your tips were very helpful

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Files and function problem! Help needed
    By sion5 in forum C++ Programming
    Replies: 10
    Last Post: 01-12-2007, 02:04 AM
  3. Binary files
    By Lionmane in forum C Programming
    Replies: 35
    Last Post: 08-25-2005, 02:56 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. problem about static Lib and function pointer
    By wu7up in forum C Programming
    Replies: 3
    Last Post: 02-24-2003, 09:34 AM

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