Namespaces
Variants

std:: fsetpos

From cppreference.net
< cpp ‎ | io ‎ | c
헤더 파일에 정의됨 <cstdio>
int fsetpos ( std:: FILE * stream, const std:: fpos_t * pos ) ;

C 파일 스트림 stream 의 파일 위치 지시자와 멀티바이트 파싱 상태(있는 경우)를 pos 가 가리키는 값에 따라 설정합니다.

새로운 파싱 상태와 위치를 설정하는 것 외에, 이 함수를 호출하면 std::ungetc 의 효과를 취소하고, 설정된 경우 end-of-file 상태를 지웁니다.

읽기 또는 쓰기 오류가 발생하면, 스트림의 오류 표시자( std::ferror )가 설정됩니다.

목차

매개변수

stream - 수정할 파일 스트림
pos - 동일한 파일과 연결된 스트림에서 std::fgetpos 를 호출하여 얻은 fpos_t 객체에 대한 포인터

반환값

0 성공 시, 그렇지 않으면 0이 아닌 값을 반환합니다. 또한 실패 시 errno 를 설정합니다.

참고 사항

넓은 스트림에서 끝이 아닌 위치로 탐색한 후, 다음 출력 함수 호출은 파일의 나머지 부분을 정의되지 않은 상태로 만들 수 있습니다. 예를 들어, 다른 길이의 멀티바이트 시퀀스를 출력함으로써 발생할 수 있습니다.

예제

#include <cstdio>
#include <cstdlib>
int main()
{
    // 부동 소수점 값 배열 준비
    const int SIZE = 5;
    double A[SIZE] = {1., 2., 3., 4., 5.};
    // 배열을 파일에 쓰기
    std::FILE * fp = std::fopen("test.bin", "wb");
    std::fwrite(A, sizeof(double), SIZE, fp);
    std::fclose(fp);
    // 값을 배열 B로 읽어오기
    double B[SIZE];
    fp = std::fopen("test.bin", "rb");
    std::fpos_t pos;
    if (std::fgetpos(fp, &pos) != 0)      // 현재 위치: 파일 시작점
    {
       std::perror("fgetpos()");
       std::fprintf(stderr, "fgetpos() failed in file %s at line # %d\n",
                    __FILE__, __LINE__-3);
       std::exit(EXIT_FAILURE);
    }
    int ret_code = std::fread(B, sizeof(double), 1, fp);      // 하나의 값 읽기
    // 현재 위치: 하나의 값을 읽은 후
    std::printf("%.1f; read count = %d\n", B[0], ret_code);   // 하나의 값과 ret_code 출력
    if (std::fsetpos(fp, &pos) != 0)   // 현재 위치를 파일 시작점으로 재설정
    {
       if (std::ferror(fp))
       {
          std::perror("fsetpos()");
          std::fprintf(stderr, "fsetpos() failed in file %s at line # %d\n",
                       __FILE__, __LINE__-5);
          std::exit(EXIT_FAILURE);
       }
    }
    ret_code = std::fread(B, sizeof(double), 1, fp);         // 첫 번째 값 재읽기
    std::printf("%.1f; read count = %d\n", B[0], ret_code);  // 하나의 값과 ret_code 출력
    std::fclose(fp);
    return EXIT_SUCCESS; 
}

출력:

1.0; read count = 1
1.0; read count = 1

참고 항목

파일 위치 표시자를 얻음
(함수)
현재 파일 위치 표시자를 반환
(함수)
파일 위치 표시자를 파일의 특정 위치로 이동
(함수)
C documentation for fsetpos