Thread: What's wrong with my fseek() ?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    What's wrong with my fseek() ?

    Dear all:

    Below will only print to Record #49 and encounters segmentation fault, I found that if I take the second fseek() off, everything works fine, can anyone enlighten me as to why the second fseek() won't work, thanks


    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/mman.h>
    #include <fcntl.h>
    
    typedef struct {
    	int integer;
    	char string[12];
    } RECORD;
    
    #define NRECORDS (50)
    
    int main () {
    
    	RECORD record;
    	int i;
    	FILE *fp;
    
    	fp = fopen("records.dat", "w+");
    	for (i=0; i<NRECORDS; i++) {
    		record.integer = i;
    		sprintf(record.string, "RECORD - %d", i);
    		fprintf(stdout, "RECORD - %d\n", i);
    		fwrite(&record, sizeof(record), 1, fp);
    	}
    
    	fclose(fp);
    
    	fp = fopen("records.dat", "w+");
    
    	fseek(fp, 43*sizeof(record), SEEK_SET);
    	fread(&record, sizeof(record), 1, fp);
    
    	record.integer = 999;
    	sprintf(record.string, "RECORD - %d\n", record.integer);
    
    	fseek(fp, 43*sizeof(record), SEEK_SET);
            // it dies here
    
    	printf("after setting to 999 \n");
    	fwrite(&record, sizeof(record), 1, fp);
    
    	fclose(fp);
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It only goes to 49 because you initialized the file to 50 elements... which like arrays are numbered 0 to 49.
    When you try to read #50, you are reading beyond the end of the file and it's going to error off.

    Also you have a problem with your sprintf() immediately above where you marked. You have not allocated enough space for record.string. As it is, you have room only for a single digit number before it overflows. Try expanding it to 20 or more characters. (A bit of slack space is less of a problem than not having enough.)

    Finally, you should take the \n out of your sprintf()s. If you want newlines, do that at display time.
    Last edited by CommonTater; 07-24-2011 at 08:06 AM.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    37
    Thanks for the reply.

    The funny thing is, I put the exactly same code on Suse Linux 64 bit server, it executes fine, but it complains about segmentation fault when I execute in Ubuntu 11.04

    Any ideas?

    Thanks

    Quote Originally Posted by CommonTater View Post
    It only goes to 49 because you initialized the file to 50 elements... which like arrays are numbered 0 to 49.
    When you try to read #50, you are reading beyond the end of the file and it's going to error off.

    Also you have a problem with your sprintf() immediately above where you marked. You have not allocated enough space for record.string. As it is, you have room only for a single digit number before it overflows. Try expanding it to 20 or more characters. (A bit of slack space is less of a problem than not having enough.)

    Finally, you should take the \n out of your sprintf()s. If you want newlines, do that at display time.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by barramundi9 View Post
    The funny thing is, I put the exactly same code on Suse Linux 64 bit server, it executes fine, but it complains about segmentation fault when I execute in Ubuntu 11.04
    Segfaults are evil like that. You get segfaults when you try to touch memory that's already allocated to something else. If you make a mistake and you're lucky you'll get a segfault. If you're unlucky your attempt to access memory you have no right to will end up accessing memory not allocated to anything and the program will run, possibly spouting a whole lot of gibberish nonsense results based on the random leftovers there.

    Frequently segfaults turn up far away from where your actual coding error was, which is highly frustrating and leads you to point an accusing finger at perfectly blameless code.

    I hate segfaults.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    37
    Well, I tried your suggestion on expanding the number of the character array element to 20 and it worked on Ubuntu as well ...

    Thanks.


    Quote Originally Posted by barramundi9 View Post
    Thanks for the reply.

    The funny thing is, I put the exactly same code on Suse Linux 64 bit server, it executes fine, but it complains about segmentation fault when I execute in Ubuntu 11.04

    Any ideas?

    Thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The funny thing is, I put the exactly same code on Suse Linux 64 bit server, it executes fine, but it complains about
    > segmentation fault when I execute in Ubuntu 11.04
    > Any ideas?
    Yes, exactly for the reason that CommonTater said.
    You overwrote the end of an array.

    The thing you have to remember is that as soon as you do something bad, absolutely ANYTHING can happen, which specifically includes "doing exactly what I expect". This is unfortunate, since you suddenly believe your program is bug free (when it isn't).
    It's only when you do something different (add more code, try another file, try another system) that it all breaks down.

    Try
    valgrind myprog
    and see what it reports as well.
    I've posted several times on how to analyse valgrind output, look for it.
    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. New to 'C' using fseek(), please help
    By leopardforest@g in forum C Programming
    Replies: 9
    Last Post: 05-09-2007, 04:53 PM
  2. Reg. fseek
    By pokks in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 01:28 PM
  3. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  4. fseek
    By Max in forum C Programming
    Replies: 5
    Last Post: 12-15-2003, 03:21 PM
  5. fseek ???
    By davie_scotland in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 06:13 PM