Thread: reading then displaying some data from a random access file

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    reading then displaying some data from a random access file

    Code:
    void show(void)
    {
    	FILE       *fptr;
    	long	   i,j=0;
    	
    	struct info rec={0,"",""};
    
    	if ( (fptr=fopen("file.dat", "rb+"))== NULL )
    	{
    		printf("File could not be opened.");
    	}
    	else
    	{
    		
    		for(k=0; feof(fptr)==0; k++)
    		{
    	
    			fseek(fptr, k * (sizeof(struct info)), SEEK_SET);
    			fread(&rec, sizeof(struct info), 1, fptr);
    			
    
    			if (rec.ID!=0)
    			{
    				printf("Your ID:%u\nYour name:%s\nYour job:%s\n", rec.ID,  rec.name,  rec.work);
    			}
    
    		}
    	
    		fclose(fptr);
    	}
    }
    There is a function that adds structures to the file, there is also one that deletes ones, etc...
    now I have a problem with displaying all the records (ie. structures) there, so how do I do so?
    There is something wrong about the code and I can`t get it!



    EDIT: I think SEEK_SET could be replaced with SEEK_CUR, like this:
    Code:
    		for(; feof(fptr)==0; )
    		{
    	
    			fseek(fptr, (sizeof(struct info)), SEEK_CUR);
    			fread(&rec, sizeof(struct info), 1, fptr);
    but this doesn`t work also, and I can`t get why!
    Last edited by kool_hamster; 01-21-2010 at 10:01 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kool_hamster View Post
    now I have a problem with displaying all the records (ie. structures) there, so how do I do so?
    There is something wrong about the code and I can`t get it!

    EDIT: I think SEEK_SET could be replaced with SEEK_CUR, like this:
    There is no need to use fseek at all.

    Code:
    			fseek(fptr, k * (sizeof(struct info)), SEEK_SET);
    			fread(&rec, sizeof(struct info), 1, fptr);
    So the first time thru, k is zero so we start at the beginning of the file. Does the first record print okay?

    The next pass, fptr is already sizeof(struct info) bytes into the file, because that's what was just read with it. Presuming the structs are packed one after the other, you shouldn't need to move it "manually" with fseek.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to check the return results of fseek and fread.

    Does your struct contain only arrays of chars (good) or char pointers (very bad)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    "There is no need to use fseek at all.
    The next pass, the fptr is already sizeof(struct info) into the file. You don't need to move it. "

    Thank you : ), now I removed the fseek.

    "So the first time thru, k is zero so we start at the beginning of the file. Does the first record print okay?"
    Yes, I tried to modify my code like this:-


    1st time:-
    Code:
    for(k=0; feof(fptr)==0 && k<8; k++)
    	fread(&rec, sizeof(struct info), 1, fptr);
    //This worked. and all what it printed was empty records (they are actually empty)

    2nd time:-
    Code:
    for(k=0; feof(fptr)==0 && k<9; k++)
    	fread(&rec, sizeof(struct info), 1, fptr);
    //This didn`t. It printed the empty records, but it couldn`t print record number 9 (which starting from zero is 8). 
    //The program crashed at this part.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    You need to check the return results of fseek and fread.

    Does your struct contain only arrays of chars (good) or char pointers (very bad)?
    char pointers, but the whole program worked fine with them. :s

    I`m thinking of a solution, it may work, but it`s not like "standard" or "neat", so is there a known way around this problem?

    EDIT:
    But I don`t understand, if it`s typically working with records, why would it matter if what is inside the record is varying in size?
    Last edited by kool_hamster; 01-21-2010 at 10:40 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char pointers, but the whole program worked fine with them. :s
    Yeah, right.

    You write out a pointer, quit the program and re-start it.
    Is that pointer still valid?
    You read the pointer back in from the disk, and it's pointing to la-la-land.

    It might work in a simple static test, but that's just dumb luck.
    But as soon as it gets more complicated, and you start pointing to either stack arrays or dynamically allocated memory, then you're on losing ground.


    Put another way, load your data file into a hex editor - do you see your strings?
    If not, you're SOL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 07:02 AM
  3. Reading from a file and using the data
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 07:44 AM
  4. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM