Last active
June 2, 2018 09:51
-
-
Save sudhanshuptl/a1d1f4bc6d105aade1538204018e87a6 to your computer and use it in GitHub Desktop.
example of fseek and ftell function c file handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudhanshu|patel||1234 | |
Chandan|kumar|54645 | |
Ashutosh|Dwivedi|34654 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
#include<stdlib.h> | |
/* | |
fseek used to move file pointer where u want to move | |
ftell used to find is current location | |
location is defined by character by character | |
like if file contains "Sudhanshu" | |
minimum position is 0 | |
and max is len("Sudhanshu") | |
*/ | |
/* fseek(file pointer, offeset, origin) | |
/* | |
origin will be : | |
SEEK_SET Beginning of file | |
SEEK_CUR Current position of the file pointer | |
SEEK_END End of file * | |
*/ | |
/* Prototypes */ | |
char *ParseLine(char *arr, char *delimiter); | |
int main(){ | |
FILE *fp; | |
char line[50]; | |
fp = fopen("data.txt","r"); | |
if(fp == NULL){ | |
printf(" Error in opening File !\n"); | |
exit(1); | |
} | |
//fetch a ParseLine | |
fscanf(fp,"%s",line); //parse the fist line that has 21 character so it point to 21 | |
/* Printing position of file pointer */ | |
//posion is n if fp point to nth char of file | |
printf("currrent posion of fp pointer in file : %ld\n", ftell(fp)); | |
printf("\nmovienf pointer to the end of FILE\n"); | |
fseek(fp, 0, SEEK_END); | |
printf("currrent posion of fp pointer in file : %ld\n", ftell(fp)); | |
printf("\nmovienf pointer to the -5th postion from End\n"); | |
fseek(fp, -5, SEEK_END); | |
printf("currrent posion of fp pointer in file : %ld\n", ftell(fp)); | |
printf("\nmovienf pointer to the begining of FILE\n"); | |
fseek(fp, 0, SEEK_SET); | |
printf("currrent posion of fp pointer in file : %ld\n", ftell(fp)); | |
printf("\nmovienf pointer to the 5th postion from begining\n"); | |
fseek(fp, 5, SEEK_SET); | |
printf("currrent posion of fp pointer in file : %ld\n", ftell(fp)); | |
fclose(fp); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
currrent posion of fp pointer in file : 21 | |
movienf pointer to the end of FILE | |
currrent posion of fp pointer in file : 65 | |
movienf pointer to the -5th postion from End | |
currrent posion of fp pointer in file : 60 | |
movienf pointer to the begining of FILE | |
currrent posion of fp pointer in file : 0 | |
movienf pointer to the 5th postion from begining | |
currrent posion of fp pointer in file : 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment