This is for a project for everyone's awareness. It's my first project in C and have a question regarding
lseek()
lseek
incompatible pointer to integer conversion...
int main(int argc, const char * argv[]) {
FILE *fp;
if((fp = fopen(argv[1], "r+b")) == NULL){
printf("Error: Unable to open file.\n");
exit(0);
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
rewind(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);
fclose(fp);
return 0;
}
lseek
int
FILE
lseek
rewind(fp);
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?
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);