Thread: Problem with fseek

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Problem with fseek

    I am trying to use fseek to move to a certainer record in a file but I am getting the runtime error "Unhandled exception at 0x7c91b1fa in accounts.exe 0xC0000005: Access violation writing location 0xccccccdc." I opened the file using
    Code:
    fp = fopen("accountsfile", "w+");
    and I am trying to seek using

    Code:
    fseek(fp, hash*sizeof(AccountRecord), 0);
    please help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are you sure "0" is a valid last argument to fseek? I always use one of the recommended macros, to remain standards compliant.

    Do you have write permission on the file?

    What is the value of "hash"?
    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
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Yes, 0 is a valid argument. The value of hash in this instance is 7. The problem seems to come from passing the file pointer to a function. For some reason, when I pass the file pointer, it changes. I am not sure why.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Just for clarity, this is from stdio.h, and yes you should place SEEK_SET instead of 0.
    Code:
    #define SEEK_CUR    1
    #define SEEK_END    2
    #define SEEK_SET    0
    Also, I am going to take a wild guess you are creating a hashing class/function where you want to place things in to a "slot", "bin", or "bucket" or whatever you want to call it, inside a file. The problem is that you cannot call fopen(blah blah blah, "w+"); and then seek somewhere. When you open a file for reading+writing it assumes you are writing to the end of the file, and if you seek to somewhere prior you can only read, not write. If you are wanting to write a portion of something to the middle of the file you have to save everything before it, and after it, then rewrite the entire file.

    One other thing to make clear is that if the file already exists, its contents are erased and truncated to zero.

    www.cplusplus.com - fopen()

    About the value of the pointer changing, you should check for NULL-ness before attempting to write or read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM