Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/c/rewind"

From cppreference.com
< cpp‎ | io‎ | c
m (Shorten template names. Use {{lc}} where appropriate.)
m (linked the `ungetc`)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{cpp/title | rewind }}
+
{{cpp/title|rewind }}
 
{{cpp/io/c/navbar}}
 
{{cpp/io/c/navbar}}
{{ddcl | header=cstdio |
+
{{ddcl|header=cstdio|
 
void rewind( std::FILE* stream );
 
void rewind( std::FILE* stream );
 
}}
 
}}
  
Moves the file position indicator to the beginning of the given file stream as if by {{c|std::fseek(stream, 0L, SEEK_SET)}}, and clears the end-of-file and the error indicators on {{tt|stream}}.
+
Moves the file position indicator to the beginning of the given file stream.
 +
 
 +
The function is equivalent to {{c|std::fseek(stream, 0, SEEK_SET);}}, except that end-of-file and error indicators are cleared.
 +
 
 +
The function drops any effects from previous calls to {{lc|std::ungetc|ungetc}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | stream | file stream to modify}}
+
{{par|stream|file stream to modify}}
 
{{par end}}
 
{{par end}}
  
Line 17: Line 21:
 
===Example===
 
===Example===
 
{{example|
 
{{example|
| code=
+
|code=
| output=
+
#include <array>
 +
#include <cstdio>
 +
 
 +
int main()
 +
{
 +
    std::FILE* f = std::fopen("file.txt", "w");
 +
    for (char ch = '0'; ch <= '9'; ch++)
 +
        std::fputc(ch, f);
 +
    std::fclose(f);
 +
 
 +
    std::array<char, 20> str;
 +
    std::FILE* f2 = std::fopen("file.txt", "r");
 +
 
 +
    const unsigned size1 = std::fread(str.data(), 1, str.size(), f2);
 +
    std::puts(str.data());
 +
    std::printf("size1 = %u\n", size1);
 +
 
 +
    std::rewind(f2);
 +
 
 +
    const unsigned size2 = std::fread(str.data(), 1, str.size(), f2);
 +
    std::puts(str.data());
 +
    std::printf("size2 = %u", size2);
 +
 
 +
    std::fclose(f2);
 +
}
 +
|output=
 +
0123456789
 +
size1 = 10
 +
0123456789
 +
size2 = 10
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/io/c/dcl list fseek}}
+
{{dsc inc|cpp/io/c/dsc fseek}}
{{dsc see c | c/io/rewind}}
+
{{dsc see c|c/io/rewind}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/io/c/rewind]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/io/c/rewind]]
+
[[fr:cpp/io/c/rewind]]
+
[[it:cpp/io/c/rewind]]
+
[[ja:cpp/io/c/rewind]]
+
[[pt:cpp/io/c/rewind]]
+
[[ru:cpp/io/c/rewind]]
+
[[zh:cpp/io/c/rewind]]
+

Latest revision as of 09:24, 30 November 2022

 
 
 
C-style I/O
Types and objects
Functions
File access
Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
Defined in header <cstdio>
void rewind( std::FILE* stream );

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

The function is equivalent to std::fseek(stream, 0, SEEK_SET);, except that end-of-file and error indicators are cleared.

The function drops any effects from previous calls to ungetc.

Contents

[edit] Parameters

stream - file stream to modify

[edit] Return value

(none)

[edit] Example

#include <array>
#include <cstdio>
 
int main()
{
    std::FILE* f = std::fopen("file.txt", "w");
    for (char ch = '0'; ch <= '9'; ch++)
        std::fputc(ch, f);
    std::fclose(f);
 
    std::array<char, 20> str;
    std::FILE* f2 = std::fopen("file.txt", "r");
 
    const unsigned size1 = std::fread(str.data(), 1, str.size(), f2);
    std::puts(str.data());
    std::printf("size1 = %u\n", size1);
 
    std::rewind(f2);
 
    const unsigned size2 = std::fread(str.data(), 1, str.size(), f2);
    std::puts(str.data());
    std::printf("size2 = %u", size2);
 
    std::fclose(f2);
}

Output:

0123456789
size1 = 10
0123456789
size2 = 10

[edit] See also

moves the file position indicator to a specific location in a file
(function) [edit]
C documentation for rewind