Using File Pointers for Navigation
A file pointer in C is a special variable of type FILE * that keeps track of your current position within an open file. When you read from or write to a file, the file pointer moves through the file's contents, ensuring that your operations happen at the correct location. Navigating within a file—jumping forward, backward, or returning to the beginning—relies on manipulating this file pointer. Mastering file pointer navigation allows you to efficiently access and modify data anywhere in a file, not just sequentially from start to end.
main.c
sample.txt
1234567891011121314151617181920212223#include <stdio.h> int main() { FILE *fp = fopen("sample.txt", "r"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } // Move file pointer to 5 bytes from the beginning fseek(fp, 5, SEEK_SET); // Tell current position long pos = ftell(fp); printf("Current file position: %ld\n", pos); // Read and print the next character int c = fgetc(fp); printf("Character at position %ld: %c\n", pos, c); fclose(fp); return 0; }
Moves the file pointer to a specific location within the file. You specify the number of bytes to move and a reference point (SEEK_SET for the beginning, SEEK_CUR for the current position, or SEEK_END for the end of the file);
Returns the current position of the file pointer as a long integer, indicating how many bytes from the beginning of the file the pointer is;
Sets the file pointer back to the very beginning of the file, making it easy to reread or rewrite the file from the start.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 12.5
Using File Pointers for Navigation
Sveip for å vise menyen
A file pointer in C is a special variable of type FILE * that keeps track of your current position within an open file. When you read from or write to a file, the file pointer moves through the file's contents, ensuring that your operations happen at the correct location. Navigating within a file—jumping forward, backward, or returning to the beginning—relies on manipulating this file pointer. Mastering file pointer navigation allows you to efficiently access and modify data anywhere in a file, not just sequentially from start to end.
main.c
sample.txt
1234567891011121314151617181920212223#include <stdio.h> int main() { FILE *fp = fopen("sample.txt", "r"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } // Move file pointer to 5 bytes from the beginning fseek(fp, 5, SEEK_SET); // Tell current position long pos = ftell(fp); printf("Current file position: %ld\n", pos); // Read and print the next character int c = fgetc(fp); printf("Character at position %ld: %c\n", pos, c); fclose(fp); return 0; }
Moves the file pointer to a specific location within the file. You specify the number of bytes to move and a reference point (SEEK_SET for the beginning, SEEK_CUR for the current position, or SEEK_END for the end of the file);
Returns the current position of the file pointer as a long integer, indicating how many bytes from the beginning of the file the pointer is;
Sets the file pointer back to the very beginning of the file, making it easy to reread or rewrite the file from the start.
Takk for tilbakemeldingene dine!