This example opens a file for reading. After performing input operations (not shown), fseek moves the file pointer to the beginning of the file.
#include <stdio.h>
#define FILENAME "myfile.dat"
#define MAX_LEN 10
int main(void)
{
FILE *stream;
char buffer[MAX_LEN + 1];
int result;
int i;
char ch;
stream = fopen("FILENAME", "r");
for (i = 0; (i < (sizeof(buffer)-1) &&
((ch = fgetc(stream)) != EOF) && (ch != '\n')); i++)
buffer[i] = ch;
result = fseek(stream, 0L, SEEK_SET); /* moves the pointer to the */
/* beginning of the file */
if (result == 0)
printf("Pointer successfully moved to the beginning of the file.\n");
else
printf("Failed moving pointer to the beginning of the file.\n");
return 0;
/*********************************************************************
The output should be:
Pointer successfully moved to the beginning of the file. *********************************************************************/ }