Pwrcdr87 Pwrcdr87 - 5 months ago 41
C Question

lseek() Trying to use with a byte file but pointer is of FILE type

This is for a project for everyone's awareness. It's my first project in C and have a question regarding

and moving the file pointer.

Right now I'm able to read the bitmap and DIB header of a bitmap file. I need to now traverse the pixels and manipulate them in certain ways. I have written out in pseudocode how I plan to tackle the manipulation. But I am having difficulty understanding how to properly use
, as I keep getting
incompatible pointer to integer conversion...
warnings with my code. I'll give a short example of my main method since this is for a project:

int main(int argc, const char * argv[]) {

FILE *fp;

if((fp = fopen(argv[1], "r+b")) == NULL){
printf("Error: Unable to open file.\n");

fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);

char test; // simply to test overwriting the current byte

fread(&test, sizeof(char), 1, fp);
test = ~test;
lseek(fp, -1, SEEK_CUR); //produces error
//also tried fseek prior to realizing I should be using lseek to move my pointer.
fwrite(&test, 1, sizeof(char), fp);

return 0;

Again, not trying to provide too much of my code since this is a project. But I'd like help understanding how to properly use
please. I noticed that it returns an
value, so I know that it is because my file pointer is of type
. But what is the proper way to use this method? I saw one example that had opened the same file in both read and write mode, where write mode was using
. But for some reason I don't know if that is correct or not.

Based on two members response, I changed my code above to:

lseek(fileno(fp), hdr.offset, SEEK_CUR); //hdr.offset == 54 bytes
printf("FD FILENO: %d\n", fileno(fp)); // prints 3???
printf("CURRENT POS: %p\n", fp); //prints 0x7fffe7eae0b0 (I understand it's an address)
fread(&test, sizeof(char), 1, fp);
lseek(fileno(fp), -1, SEEK_CUR);
fwrite(&test, 1, sizeof(char), fp);
printf("CURRENT POS: %p\n", fp); //prints the same address as above?

What am I not getting other than everything to do with C?

Answer Source

If you'd like to keep using FILE *fp, which is probably a good idea, you can get convert fp to its corresponding int file descriptor with int fileno(FILE *stream). In other words,

lseek(fileno(fp), -1, SEEK_CUR); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download