This commit is contained in:
Saturneric 2020-12-08 12:24:52 +08:00
parent 89e1c349ef
commit 2f3eed8ac5
5 changed files with 126 additions and 0 deletions

6
5/io/fmemopen.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void) {
// TODO
return 0;
}

45
5/io/fopen.c Normal file
View File

@ -0,0 +1,45 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
int main(void) {
int fd = open("test/filefp", O_RDWR | O_CREAT | O_TRUNC, RWRR);
if(fd == -1) {
printf("open file error filefp\n");
return -1;
}
FILE *fp = fdopen(fd, "w+");
char buf[1024];
if(setvbuf(fp, buf, _IOFBF, 1024)) {
printf("setvbuf error\n");
return -1;
}
if(ferror(fp)) {
printf("io stream error\n");
return -1;
}
if(feof(fp)) {
printf("arrive the end of the file\n");
}
putc('H', fp);
fputc('Y', fp);
fputs(" Hello World!", fp);
fflush(fp);
fclose(fp);
close(fd);
return 0;
}

40
5/io/fread.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void) {
FILE *fp = fopen("./test/filefp", "r+");
char buf[1024];
char *pbuf = fgets(buf, 1024, fp);
printf("buf: %s\n", buf);
fseek(fp, 0, SEEK_END);
fprintf(fp, "num: %d", 123456);
fpos_t off;
fgetpos(fp, &off);
printf("off: %lu\n", off);
int fd = fileno(fp);
struct stat st;
if(fstat(fd, &st) < 0) {
printf("stat error fd\n");
return -1;
}
printf("dev: %ld\n", st.st_dev);
fclose(fp);
close(fd);
return 0;
}

1
5/io/test/filefp Normal file
View File

@ -0,0 +1 @@
HY Hello World!num: 123456num: 123456num: 123456

34
5/io/tmpfile.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
int main(void) {
char buf[L_tmpnam];
char *pbuf = tmpnam(buf);
printf("tmpnam: %s\n", pbuf);
FILE *fp = tmpfile();
if(fp == NULL) {
printf("create tmp file error\n");
return -1;
}
fputs("Hellow World!", fp);
rewind(fp);
char buf2[1024];
if(fgets(buf2, sizeof(buf2), fp) == NULL){
printf("fgets error\n");
return -1;
}
fputs(buf2, stdout);
fclose(fp);
return 0;
}