Untitled
Untitled
r Open a text file for reading only. If the file doesn’t exist, it returns
void main()
{
FILE *fp; int ch; int r = 0;
fp = fopen(“item.c”, “r+”);
if(fp==NULL)
{
printf(“Unable to open
item.c”);
}
}
else{
do{
fread(&it, sizeof(it),1,fp);
printf(“\n%d %s %d %f”, it.itid, it.n, it.qty, it.rate);
printf(“\n Press 1 to change it?”);
scanf(“%d”,&ch);
if(ch==1)
{
printf(“\n Enter Itemid ItemName Quantity & Price”);
scanf(“%d%s%d%f”, &it.itid, it.n, &it.qty, &it.rate);
fseek(fp, r*sizeof(it), 0);
fwrite(&it, sizeof(it),1,fp);
}r++;
}while(!feof(fp));
fclose(fp);
}
}
fseek(FILE *fptr, long offset, int reference)
Moves the pointer from one record
to another.
The first argument is the file pointer
The second argument tells the compiler how
many bytes (offset) the pointer should be
moved from a particular position.
The third argument is the reference from
where the pointer should be moved n bytes
(specified in the offset).
fseek(FILE *fptr, long offset, int reference)
The third argument is the reference from
where the pointer should be moved n bytes
as specified in the offset.
SEEK_END moves the pointer from the
end marker
SEEK_CUR moves the pointer from the
current position
SEEK_SET moves the pointer from the beginning
of the file
fseek(FILE *fptr, long offset, int reference)
Example