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);}
- 01.文件i/o编程之一
- Linux文件I/O之一
- 文件I/O编程
- 文件I/O编程
- 文件I/O编程
- 文件I/O编程
- 文件I/O操作编程
- linux 文件I/O 编程
- 标准I/O文件编程
- Linux 文件I/O编程
- linux文件I/O编程
- linux文件I/O编程
- UNIX环境高级编程(第三版)学习笔记(三、文件I/O之一)
- Linux系统文件I/O编程(三)---I/O多路复用
- Linux系统文件I/O编程(三)---I/O多路复用
- Hadoop I\O之一
- unix环境高级编程之一( 基本I/O函数)
- matlab编程 文件的I/O
- 高次同余方程式的解数及解法
- iOS 归档的使用
- SELECT语句中执行条件逻辑( 返回两个日期之间的天数 )
- Secure CRT Function
- Linux 文件I/O(三)之fcntl记录锁
- 01.文件i/o编程之一
- 二叉数面试题进阶
- ASP.NET MVC 入门5、View与ViewData
- Android_launcher的源码详细分析
- 【热门主题:5代火影黑色主题】
- sota系统服务进程的启动与停止(章节:4.2)
- IE firefox 浏览器禁用后退按钮 javascript
- 高尔夫7 1.4T高功率版 没有革命性的变革
- 了解sota字符界面(章节4.1)