MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / libc_fseek

Function libc_fseek

examples/libc/file.c:63–519  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61}
62
63int libc_fseek(void)
64{
65 const char *tmpdir;
66 char *fname;
67 int fd;
68 FILE *fp;
69 const char outstr[] = "hello world!\n";
70 char strbuf[sizeof outstr];
71 char buf[200];
72 struct stat st1;
73 struct stat st2;
74 int result = 0;
75
76 tmpdir = getenv("TMPDIR");
77 if (tmpdir == NULL || tmpdir[0] == '\0')
78 tmpdir = "/tmp";
79
80 asprintf(&fname, "%s/tst-fseek.XXXXXX", tmpdir);
81 if (fname == NULL)
82 {
83 fprintf(stderr, "cannot generate name for temporary file: %s\n",
84 strerror(errno));
85 return 1;
86 }
87
88 /* Create a temporary file. */
89 fd = mkstemp(fname);
90 if (fd == -1)
91 {
92 fprintf(stderr, "cannot open temporary file: %s\n", strerror(errno));
93 return 1;
94 }
95
96 fp = fdopen(fd, "w+");
97 if (fp == NULL)
98 {
99 fprintf(stderr, "cannot get FILE for temporary file: %s\n", strerror(
100 errno));
101 return 1;
102 }
103 setbuffer(fp, strbuf, sizeof(outstr) - 1);
104
105 if (fwrite(outstr, sizeof(outstr) - 1, 1, fp) != 1)
106 {
107 printf("%d: write error\n", __LINE__);
108 result = 1;
109 goto out;
110 }
111
112 /* The EOF flag must be reset. */
113 if (fgetc(fp) != EOF)
114 {
115 printf("%d: managed to read at end of file\n", __LINE__);
116 result = 1;
117 }
118 else if (!feof(fp))
119 {
120 printf("%d: EOF flag not set\n", __LINE__);

Callers

nothing calls this directly

Calls 8

timeFunction · 0.85
sleepFunction · 0.70
printfFunction · 0.50
fgetcFunction · 0.50
lseekFunction · 0.50
fputcFunction · 0.50
fstatFunction · 0.50
unlinkFunction · 0.50

Tested by

no test coverage detected