How to use SEEK_CUR on FILE *

offset=ftell(ptr)-sizeof(student1); fseek(ptr,offset,SEEK_SET); fwrite(&student1,sizeof(student1),1,ptr); 

This C code means moving the pointer from the current ftell(ptr) position to start the just-read structure block. I'm right?

If I am right, can I use SEEK_CUR instead of SEEK_SET to return to the beginning of the structure block in the file?

Please show me how to use SEEK_CUR and go to the beginning of the structure block.

I am new to programming. So please kindly help me.

Edit: Thanks for the answers. What I'm trying to do is search for a keyword (student’s roll number) and update information about this student (name, address, ..). Updated data replaces previous data successfully. Let me ask one more question. Is there a way to insert new data over previous data instead of replacing it with old data?

+4
source share
2 answers

This C code means moving the pointer from the current position [ftell (ptr)] to start the just-read structure block. I'm right?

I think so.

Please show me how to use SEEK_CUR and return to the beginning of the structure block.

You can use a negative bias.

 #include <stdio.h> fseek (ptr, -sizeof student1, SEEK_CUR); 

In any case, you should avoid these calls; it can be very slow. Use a fairly consistent read.

+7
source

try:

 fseek(ptr, -sizeof(student1), SEEK_CUR); 
+3
source

Source: https://habr.com/ru/post/1445311/


All Articles