SetFilePtr
- Updated2023-02-21
- 3 minute(s) read
ssize_t SetFilePtr (int fileHandle, ssize_t offset, int origin);
Purpose
Moves the file pointer for the file specified by fileHandle to a location that is offset bytes from origin. Returns the offset of the new file pointer position from the beginning of the file.
You can use SetFilePtr to obtain the file size by setting offset to 0 and origin to 2. In this case, the return value indicates the file size, and the file pointer points to the end of the file.
You can position the file pointer beyond the end of the file. Intermediate bytes, bytes between the old end of file and the new end of file, contain values that might vary. An attempt to position the file pointer before the beginning of the file causes the function to return an error.
If the file is a device that does not support random access, such as the Standard Input, the function returns a value that might vary.
Example Code
/* Open or create the file c:\TEST.DAT, move 10 bytes into the file, and write
a string to the file. */
/* Note: In C, use \ in pathname instead of \. */
int handle, result;
long position;
handle = OpenFile("c:\TEST.DAT", 0, 2, 1);
if (handle == –1){
FmtOut("error opening file");
exit(1);
}
position = SetFilePtr(handle, 10L, 0);
if (position == 10){
result = WriteFile(handle, "Hello, World!", 13);
if (result == -1)
FmtOut("error writing to file");
}
else
FmtOut("error positioning file pointer");
CloseFile(handle);
Parameters
Input | ||||
Name | Type | Description | ||
fileHandle | int | File handle you obtain from OpenFile that specifies the file to which the data is written. | ||
offset | ssize_t | Specifies the number of bytes to position the file pointer from the specified origin. This value may be negative. Also, you may index past the end of the file.
|
||
origin | int | Position in the file from which to base offset. The valid values of origin are as follows: 0 = beginning of file 1 = current position of file pointer 2 = end of file
|
Return Value
Name | Type | Description |
positionFromStart | ssize_t | Offset of the new file pointer position from the beginning of the file. If the value is –1, an error occurred. This error is caused by an invalid file handle, an invalid origin value, or an offset value that is before the beginning of the file. You can use GetFmtIOError to get more information about the type of error that occurred. |
Additional Information
Library: Formatting and I/O Library
Include file: formatio.h
LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later