-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfseek_example.c
More file actions
60 lines (49 loc) · 1.62 KB
/
fseek_example.c
File metadata and controls
60 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
int main()
{
int input_char;
FILE *my_stream;
char my_filename[] = "snazzyjazz.txt";
long position;
int eof_status, error_status, close_error;
my_stream = fopen (my_filename, "w");
fprintf (my_stream, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
/* Close stream; skip error-checking for brevity of example */
fclose (my_stream);
printf ("Opening file...\n");
my_stream = fopen (my_filename, "r");
position = ftell (my_stream);
input_char = getc (my_stream);
printf ("Character at position %d = '%c'.\n\n", position, input_char);
printf ("Seeking position 25...\n");
fseek (my_stream, 25, SEEK_SET);
position = ftell (my_stream);
input_char = getc (my_stream);
printf ("Character at position %d = '%c'.\n\n", position, input_char);
printf ("Attempting to read again...\n");
input_char = getc (my_stream);
eof_status = feof (my_stream);
printf ("feof returns %d.\n\n", eof_status);
error_status = ferror (my_stream);
printf ("ferror returns %d.\n", error_status);
printf ("Attempting to write to this read-only stream...\n");
putc ('!', my_stream);
error_status = ferror (my_stream);
printf ("ferror returns %d.\n\n", error_status);
printf ("Rewinding...\n");
rewind (my_stream);
position = ftell (my_stream);
input_char = getc (my_stream);
printf ("Character at position %d = '%c'.\n", position, input_char);
close_error = fclose (my_stream);
/* Handle fclose errors */
if (close_error != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
return 0;
}