Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | io‎ | c
m (Update links.)
Line 5: Line 5:
 
}}
 
}}
  
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 {{cc|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|ungetc}}.
  
 
===Parameters===
 
===Parameters===
Line 18: Line 22:
 
{{example|
 
{{example|
 
| code=
 
| code=
 +
#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::fopen("file.txt", "r");
 +
    std::fread(str, 1, 10, f);
 +
    std::puts(str);
 +
 +
    std::rewind(f);
 +
    std::fread(str, 1, 10, f);
 +
    std::puts(str);
 +
    std::fclose(f);
 +
}
 
| output=
 
| output=
 
}}
 
}}

Revision as of 05:55, 21 October 2013

 
 
 
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

Parameters

stream - file stream to modify

Return value

(none)

Example

#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::fopen("file.txt", "r");
    std::fread(str, 1, 10, f);
    std::puts(str);
 
    std::rewind(f);
    std::fread(str, 1, 10, f);
    std::puts(str);
    std::fclose(f);
}

See also

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