Thread: File I/O is screwed up bad?(or i am one)

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263

    File I/O is screwed up bad?(or i am one)

    ok im one to assume the problem is me but this is ridiculous or maybe im code blind or something? anyway why in the HELL does the file pointer jump 1000+ bytes into the file after a single 4 byte read?
    im completly blind or this is some ****??!?

    this is the chunk of code that does it yeah i have a proper file name and it opens fine.

    Code:
    int tpos =12,t = 0;
    char ch[4];
    FILE* fp = fopen("blablawhatever","rb");
    if(fp != NULL)
    {
        fseek(fp,tpos,SEEK_SET);
        fread(ch,4,1,fp); // this is where the File Position jumpes
        fread(&t,4,1,fp);
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    I am trying to workout what it is you are trying to do here, you open the file okay then you set the file pointer 12 * sizeof long from the start of the file, read four bytes and try to store it as a char (which is one byte), then read another four bytes (sizeof int - depending on the architecture of your system)?
    look at this
    Code:
    fseek(fp, 0L, SEEK_SET); /*START OF FILE*/
    fseek(fp, 12 * sizeof(char), SEEK_SET); /*OKAY IF YOUR FILE IS*/
    /*MADE UP OF CHARS ONLY */
    
    typedef struct {
           char ch;
           int num;
          } MYSTRUCT;
    
    MYSTUCT value;
    
    fseek(fp, 12 * sizeof(MYSTRUCT), SEEK_SET);
    fread(&value, sizeof(MYSTRUCT), 1, fp);
    the above will read the char and number in 13th position from start of the file - the desired result will only be acheived if the file is made up of records only in the same format as your structure
    Last edited by bigtamscot; 03-09-2002 at 07:05 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's most unusual, I ran a test program with the same code and it works just fine. I assume you've already stepped through your code to see if there are any unkosher values floating around when you try to read from the file? The only thing I can think of is that somehow you are corrupting your file position value.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    ok, what im doing requires sequential file access, in that i read a 4 byte tag name which i declared as ch[4](4 bytes) then read a 4 byte size(int t),

    this is a stripped down version of the code but it still always causes the error.

    > assume you've already stepped through your code to see if there are any unkosher values floating around when you try to read from the file?

    many many many many times...

    >The only thing I can think of is that somehow you are corrupting your file position value.


    thats what i thought but i see nothing in the code to do that
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, despite a lot of effort I can't duplicate the problem even with the exact same code you posted. This leads me to believe that it's something to do with your data file, though that makes even less sense. I can't do any more without the actual code and file, sorry.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i find now that the file may be corrupt. thank you very much for your help anyway, i did not expect this since file corruption is a rare problem.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ah, a corrupt file would certainly result in erratic behavior. Whew, this problem certainly gave me a mental workout trying to remember obscure errors with fseek and fread

    -Prelude
    My best code is written with the delete key.

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    yeah almost gave me a breakdown, now i got that "working" now i have some wierd memory bug(this is a code bug me thinks)! GOD I LOVE C!! ya never get a break.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM