Stupid File IO

This is a discussion on Stupid File IO within the C Programming forums, part of the General Programming Boards category; Ok, this is what I need to do. I need to write a function to open a file, get the ...

  1. #1
    Registered User 4point5's Avatar
    Join Date
    Oct 2002
    Posts
    44

    Angry Stupid File IO

    Ok, this is what I need to do. I need to write a function to open a file, get the data of 1 byte at a certain offset in the file, and store it in a variable. I have been trying to do this for the last two days and I just can't figure it out. This is what I have come up with.

    int GetData(char FileName[255])
    {
    FILE *file;
    char buff;

    file = fopen(FileName, "r");
    fseek(file, 0x3113, 0x0000);
    fread(&buff, sizeof(buff), sizeof(buff), file);
    }

    I suck...
    Don't try so hard. Just let it happen.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Maybe you need to open the file in binary mode

    Here a small example (didn't test it).
    Code:
    int GetData(char *FileName)
    {
       FILE *file;
       char buff;
       int offset = 100;
    
       file = fopen(FileName, "rb");
    
       if(file == NULL)
          return -1;
    
       if(fseek(file, offset, SEEK_SET) == -1)
       {
          fclose(file);
          return -2;
       }
    
       if(fread(&buff, 1, sizeof(buff), file) != 1)
       {
          fclose(fp);
          return -3;
       }
       printf("Your data is: %c\n", buf);
       fclose(fp);
       return 0;
       }
    }

  3. #3
    Registered User 4point5's Avatar
    Join Date
    Oct 2002
    Posts
    44
    Thanks, Monster. I got it working now.

    You guys are great!
    Don't try so hard. Just let it happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

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