answersLogoWhite

0

How do you use fseek function in c?

Updated: 8/10/2023
User Avatar

KishorR

Lvl 1
12y ago

Best Answer

Answer

First a few points.

I do not know if you intended it but you are asking this of a C++ expert not a C expert. I mention this as fseek forms part of the standard C library file operations. Of course the C++ standard library inherits these functions, but they are not directly compatible with C++ IOStreams. You therefore have to have opened the file using the C fopen function, and close it using the C fclose function.

Next, you have to be careful when using fseek with text files. Things like end of line character sequences and the size of characters used can make a difference to the position values you need to pass to fseek via its offset parameter. The fseek function positions to the requested byte position within the referenced file, it knows and cares nothing about characters or their encoding making up the data of the file.

You also have all that silliness to do with character 26 (ctrl-Z) being interpreted as end of file in text mode - a Microsoft hangover from the days of MS-DOS. Then there is the effect caused by skipping over the byte order mark (if there is one), which as far as I know has to do with text files of characters in various Unicode encodings (so hopefully can be ignored in simple cases). See http://unicode.org/unicode/faq/utf_bom.html#BOM for more information on byte order marks.

You ask about positioning to the letter positions. Not all characters are letters and not all characters are printable. As I said fseek knows and cares nothing about the data or meaning of the values in a file and therefore cannot tell a letter from any other character type.

So assuming you meant characters when you mention letters in your question and assuming characters are all the same size and are byte sized then to position to the 45th character when starting with the file pointer positioned at the 20th character you can do the following:

status = fseek( file, 44L, SEEK_SET );

The fseek call shown above assumes file is a FILE * pointing to a valid open FILE structure. 44L specifies the position as a long integer constant, SEEK_SET is a value indicating that 44L is used to specify the position counting from the beginning of the file. As the beginning of the file is at position 0 and not position 1 we have to subtract 1 from the byte number we want if we count the first character as character 1.

The status variable is an int and is used to hold the returned value which is 0 on success and some non-zero value if fseek failed. If fseek fails you should check errno for more details or call a function such as perror as in the MSDN example, which converts the errno value to a string, prefixes the passed text and displays it on stderr (as associated with std::cerr in C++).

Alternatively you could specify the position to move to relative to the current position which would be the value given by the difference between the new position from the current position, thus 45-20 = 25:

status = fseek( file, 25L, SEEK_CUR );

Here the way the position value is interpreted is specified as SEEK_CUR, meaning to count from the current file position. This assumes of course that the file pointer position is in fact at position 19 (i.e. the 20th byte counting from 0) to start with. If it were at (say) position 112 then it would end up at position 137.

The reverse would be either:

status = fseek( file, 19L, SEEK_SET );

That is move to position 20 as counted from 0, the beginning of the file.

Or:

status = fseek( file, -25L, SEEK_CUR );

Which means move to the current position less 25, which assuming the file pointer is positioned at position 44 (i.e. byte 45 counted from 0) to start with would position it to position (44-25) = 19 (i.e. byte 20 counted from 0).

If you look carefully at the signature for fseek then you would notice that the offset parameter is specified as a long value - that is it is a signed value and so can be negative.

I should note that we could also position relative to the end of the file by specifying SEEK_END as the third parameter value. However this would mean we would need to know how long the file was in bytes so we can calculate the distance from the end of the file to the positions we require and so seems to be more effort than it is worth in this case.

Hope this helps. If you require further information then please ask another question.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Use the help/manual, it does explain everything. (Note: in UNIX you might want to use fseeko, if you want Large File Support.)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use fseek function in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is a function to read chars from a certain position to another position in file in C?

Call functions fread, then function fseek, then function fwrite.


What is the difference between fseek and lseek?

lseek is a system call, but fseek is a C function belonging to the ANSI C standard library, and included in the file stdio.h lseek uses file descriptor (return by open system call), but fseek uses pointer to FILE structure (return by fopen ANSI C library function) (though file desctor and FILE * can be used interchangeably several times). System calls are to communicate directly with an operating system. Generally, system calls are slower than normal function calls.


What function returns the last but one character of end of file?

read/fread/fgetc, but beforehand you have to use lseek/llseek/fseek


Which function is used to move file pointer from any position in file in c language?

FILE-level: ftell/fseek, fgetpos/fsetpos handle-level: lseek


How do you use this function in c programme?

I don't use that function in C programme.


Can you use main function as a recursive function in C?

Yes


How do you use getpass in C?

This function is obsolete. Do not use it.


How do you detect an empty file in a c program?

Seek to the end of the file (fseek) and check how many bytes are in the file If the byte count is zero the file is empty.


Why you use main function in c?

Because if you donot use main function in c program, the compiler willnot execute the program.C compiler starts execution from main function itself.


What scrabble words use f and c?

Function


Why you use 'integer' before function in C language?

To specify the return-type of the function.


What function is used to perform exponentiation in C language?

You can use the pow() function in math.h.