|
|
다시 들쳐 보는 문법 및 레퍼함수 - fseek |
|
|
제목을 어떻게 붙일까하다가 "다시 들쳐 보는 문법 및 레퍼함수"이라 하였다. 특별히 fseek를 사용하는데 어려운 일이 있는 것도 아니다.
다만 가물 가물 해져가는 기억을 위해 실험하고 기록하여 이짓( IT )을 오래도록 해먹기 위한 발악쯤으로 생각 하면 될 듯...
fseek를 MSDN을 참조하면...
* 특정 위치로 파일의 포인터를 움직인다. * 64bit를 위한 offset이 존재 한다는 것을 알 수 있다.
msdn에서 함수에 대한 설명을 볼때 가장 중요하게 볼 것은 역시, 인자 값과, 리턴 값일 것이다. 아래 내용을 잘 살펴보면
"If successful, fseek and _fseeki64 returns 0. Otherwise, it returns a nonzero value. " 성공과 실패에 대한 return 값을 정의하고 있다.
MSDN을 살펴보면 친절하게도 예제 코드도 제공 해준다. 굿이다.
Moves the file pointer to a specified location.
int fseek(
FILE *stream,
long offset,
int origin
);
int _fseeki64(
FILE *stream,
__int64 offset,
int origin
);
Parameters
- stream
-
Pointer to FILE structure.
- offset
-
Number of bytes from origin.
- origin
-
Initial position.
Return Value
If successful, fseek and _fseeki64 returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined. If stream is a null pointer, or if origin is not one of allowed values described below, fseek and _fseeki64 invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return -1.
Remarks
The fseek and _fseeki64 functions moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:
- SEEK_CUR
-
Current position of file pointer.
- SEEK_END
-
End of file.
- SEEK_SET
-
Beginning of file.
You can use fseek and _fseeki64 to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek and _fseeki64 clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.
When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file.
For streams opened in text mode, fseek and _fseeki64 have limited use, because carriage return–linefeed translations can cause fseek and _fseeki64 to produce unexpected results. The only fseek and _fseeki64 operations guaranteed to work on streams opened in text mode are:
- Seeking with an offset of 0 relative to any of the origin values.
- Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseek or _ftelli64 when using _fseeki64.
Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using the combination of fseek and ftell or _fseeki64 and _ftelli64, to move within a file that ends with a CTRL+Z may cause fseek or _fseeki64 to behave improperly near the end of the file.
When the CRT opens a file that begins with a Byte Order Mark (BOM), the file pointer is positioned after the BOM (that is, at the start of the file's actual content). If you have to fseek to the beginning of the file, use ftell to get the initial position and fseek to it rather than to position 0.
This function locks out other threads during execution and is therefore thread-safe. For a non-locking version, see _fseek_nolock, _fseeki64_nolock.
Requirements
Function |
Required header |
Compatibility |
fseek |
<stdio.h> |
ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
_fseeki64 |
<stdio.h> |
Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For additional compatibility information, see Compatibility in the Introduction.
Example
// crt_fseek.c
// This program opens the file FSEEK.OUT and
// moves the pointer to the file's beginning.
#include <stdio.h>
int main( void )
{
FILE *stream;
char line[81];
int result;
if ( fopen_s( &stream, "fseek.out", "w+" ) != 0 )
{
printf( "The file fseek.out was not opened\n" );
return -1;
}
fprintf( stream, "The fseek begins here: "
"This is the file 'fseek.out'.\n" );
result = fseek( stream, 23L, SEEK_SET);
if( result )
perror( "Fseek failed" );
else
{
printf( "File pointer is set to middle of first line.\n" );
fgets( line, 80, stream );
printf( "%s", line );
}
fclose( stream );
}
OutputFile pointer is set to middle of first line. This is the file 'fseek.out'.
.NET Framework Equivalent
See Also
|
|
|
|
|
|
« 2025/01 »
일 |
월 |
화 |
수 |
목 |
금 |
토 |
|
|
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
|
|
Total :
Today :
Yesterday : |
|
|
|
|
|
|