01.文件i/o编程之一

来源:互联网 发布:淘宝店铺模版图片尺寸 编辑:程序博客网 时间:2024/04/29 02:53

【代码1】test_EOF.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(void){FILE *fp = NULL;if (NULL == (fp = fopen("1.txt", "r"))) {fprintf(stderr, "fopen : %s\n", strerror(errno));exit(1);}int temp;temp = fgetc(fp);while(temp != EOF) {fputc(temp, stdout);temp = fgetc(fp);}fflush(stdout);fclose(fp);return 0;}

【代码2】test_fopen.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(void){FILE *fp = NULL;if (NULL == (fp = fopen("1.txt", "a"))) {fprintf(stderr, "fopen : %s\n", strerror(errno));//fprintf(stderr, "%d\n", errno);exit(1);}fclose(fp);return 0;}

【代码3】test_freopen.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(int argc, char *argv[]){FILE *fp = NULL;printf("yyyy\n");if (NULL == (fp = freopen("1.txt", "w+", stdout))) {fprintf(stderr, "freopen : %s\n", strerror(errno));exit(1);}printf("xxxx\n");fclose(fp);exit(0);}

【代码4】test_fread.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(void){FILE *fp = NULL;if (NULL == (fp = fopen("2.txt", "w+"))) {fprintf(stderr, "%d : freopen : %s\n", __LINE__ - 1, strerror(errno));exit(1);}int ret;char buf[32];int pos;pos = ftell(fp);printf("pos is %d\n", pos);fgets(buf, sizeof(buf), stdin);printf("%s", buf);ret = fwrite(buf, 1, strlen(buf), fp);pos = ftell(fp);printf("pos is %d\n", pos);printf("fwrite : %d\n", ret);memset(buf, 0, sizeof(buf));//fseek(fp, 0, SEEK_SET);rewind(fp);pos = ftell(fp);printf("before fread pos is %d\n", pos);ret = fread(buf, 5, sizeof(buf), fp);//buf[ret] = '\0';pos = ftell(fp);printf("after fread pos is %d\n", pos);printf("fread : %d\n", ret);printf("%s", buf);fclose(fp);return 0;}

【代码5】test_getchar.c

#include <stdio.h>#include <stdlib.h>#include <stdlib.h>#include <errno.h>int main(int argc, char *argv[]){//char temp;unsigned char temp;while((temp = getchar()) != EOF) {fputc(temp, stdout);}exit(0);}

【代码6】test_setvbuf.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>int main(void){if (0 > setvbuf(stdout, NULL, _IONBF, 0)) {fprintf(stderr, "setvbuf : %s\n", strerror(errno));exit(1);}printf("xxxx");while(1);exit(0);}

【代码7】test_strchr.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(int argc, char *argv[]){char buf[] = "abcdd1sdfasdf";char *p = NULL;printf("%s\n", buf);if (NULL == (p = strchr(buf, '1'))) {fprintf(stderr, "strchr : %s\n", strerror(errno));exit(1);}printf("buf : %p\n", buf);printf("1 : %p\n", p);exit(0);}

【代码8】test_strstr.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <time.h>int main(void){/* FILE *fp = NULL; *//* time_t tm; *//* time(&tm); *//* char *p = ctime(&tm); *//* char buf[128]; *//* snprintf(buf, sizeof(buf), "%d, %s\n", 5, p); *//* printf("%s\n", buf); */char buf[] = "123abcefggggg";char *p;printf("%s\n", buf);if (NULL == (p = strstr(buf, "fg"))) {fprintf(stderr, "can not find the string!\n");exit(1);}printf("buf : %p\n", buf);printf("fg : %p\n", p);return 0;}

【代码9】test_fgets.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>#include <unistd.h>int main(void){char buf[6];while(1) {fgets(buf, sizeof(buf), stdin);printf("%s", buf);fflush(stdout);sleep(2);}exit(0);}



原创粉丝点击