C-style file input/output---file seeking---(std::ftell,std::fgetpos,std::fseek,std::fsetpos,std::rewind)

The CI/O subset of the C++ standard library implements C-style stream input/output operations. The <cstdio> header file provides general file support and provides functions with narrow and multibyte character input/output capabilities, while the <cwchar> header file provides functions with wide character input/output capabilities.

File search

Returns the current file position indicator

std::ftell

long ftell( std::FILE* stream );

streamReturns the current value of the file position indicator for file stream .

If the stream was opened in binary mode, the value obtained by this function is the number of bytes from the beginning of the file.

If the stream is opened in text mode, the return value of this function is unspecified and is only meaningful as input to std::fseek.

parameter

stream - file stream to examine

return value

File location indicator on success, -1L on failure. errno is also set on failure.

Get file location indicator

std::fgetpos

int fgetpos( std::FILE* stream, std::fpos_t* pos );

Gets streamthe file position indicator and current parsing status (if present) of the file stream and stores them in posthe object pointed to. The stored value is only meaningful as input to std::fsetpos.

parameter

stream - file stream to examine
pos - Pointer to the fpos_t object into which the file position indicator is to be stored.

return value

​0​ on success, non-zero otherwise. errno is also set on failure.

Moves the file position indicator to the specified position in the file

std::fseek

int fseek( std::FILE* stream, long offset, int origin );

Sets streamthe file position indicator of file stream to offsetthe value pointed to by .

If streamopened in binary mode, the new position is exactly bytes after the start of the file (if originSEEK_SET ) or the current file position (if originSEEK_CUR ), or the end of the file (if originSEEK_END ) offset. The binary stream is not required to support SEEK_END, especially whether to output additional null bytes.

If streamopened in text mode, the only supported offsetvalues ​​are zero (can be used with any origin) and the return value of a previous call to std::ftell on a stream associated with the same file (can be used with SEEK_SET only origin).

If streamwide-oriented, restrictions on text and binary streams apply together (results of std::ftell are allowed with SEEK_SET, and zero offsets are allowed relative to SEEK_SET and SEEK_CUR but not SEEK_END).

In addition to changing the file position indicator, fseekalso undoes the effects of std::ungetc and clears the end-of-file status, if applicable.

If a read or write error occurs, the stream's error indicator ( std::ferror ) is set without affecting the file position.

parameter

stream - file stream to modify
offset - Number of characters migrated relative to origin
origin - offsetThe added position. It can have one of the following values: SEEK_SET, SEEK_CUR, SEEK_END

return value

​0​ on success, non-zero otherwise.

Notice

After scanning to a non-end position in a wide stream, the next call to any output function may leave the remaining file contents undefined, for example by outputting a multibyte sequence of different lengths.

For text streams, offsetthe only legal values ​​are 0 (applies to any origin) and the return value of a previous ftell call (applies to only SEEK_SET).

POSIX allows polling after the end of file. If output occurs after this patrol bit, reads in any gap will return zero bytes. Where supported by the file system, this creates a sparse file .

POSIX also requires fseek to fflush first if there is any unwritten data (but whether to restore the migration state is implementation-defined). Standard C++ file streams are guaranteed to be flushed and unmigrated: std::basic_filebuf::seekoff .

Moves the file position indicator to the specified position in the file

std::fsetpos

int fsetpos( std::FILE* stream, const std::fpos_t* pos );

posSets the C file stream stream's file position indicator and multibyte parsing status, if present, according to the value pointed to by .

In addition to establishing a new parsing state and position, calling this function undoes the effects of std::ungetc and clears the end-of-file state if it was set.

If the read or write fails, the stream's error indicator ( std::ferror ) is set.

parameter

stream - file stream to modify
pos - Pointer to the fpos_t object obtained from a call to std::fgetpos on the stream associated with the same file

return value

​0​ on success, non-zero otherwise. errno is also set on failure.

Notice

After searching to a non-end position of a wide stream, the next call to any output function may leave the remaining file contents undefined, for example by outputting a multibyte sequence of different lengths.

Move the file position indicator to the beginning of the file

std::rewind

void rewind( std::FILE* stream );

Moves the file position indicator to the beginning of the given file stream.

Function equivalent to std::fseek(stream, 0, SEEK_SET); except that it clears the end-of-file and error indicators.

This function discards any effects from previous calls to ungetc.

parameter

stream - file stream to modify

return value

(none)

Call example 1

#include <cstdio>
#include <cstdint>
#include <vector>
#include <fstream>
#include <cassert>

int main()
{
    std::ofstream("dummy.nfo") << "sample data\n";


    std::FILE* fp = std::fopen("dummy.nfo", "rb");
    assert(fp);

    std::fseek(fp, 0, SEEK_END); // 寻位到结尾
    std::size_t filesize = std::ftell(fp);

    std::fseek(fp, 0, SEEK_SET); // 寻位到起始
    std::vector<uint8_t> buffer(filesize);
    std::fread(buffer.data(), sizeof(uint8_t), buffer.size(), fp);

    std::fclose(fp);
    std::printf("i've read %zi bytes\n", filesize);
    return 0;
}

output

Call example 2

#include <cstdio>

int main()
{
    std::FILE *f;
    char ch;
    char str[20];

    f = std::fopen("file.txt", "w");
    for (ch = '0'; ch <= '9'; ch++)
    {
        std::fputc(ch, f);
    }
    std::fclose(f);


    std::FILE* f2 = std::fopen("file.txt", "r");
    unsigned int size = std::fread(str, 1, 10, f2);
    std::puts(str);
    std::printf("\n%u\n", size);

    std::rewind(f2);
    unsigned int size2 = std::fread(str, 1, 10, f2);
    std::puts(str);
    std::printf("\n%u", size2);
    std::fclose(f2);
    return 0;
}

 output

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132796180