文件编程之C库函数
来源:互联网 发布:python list 去重 编辑:程序博客网 时间:2024/05/18 23:29
1、打开流
prototype:
#include <stdio.h>FILE *fopen(const char *restrict pathname, const char restrict type);
fopen 打开路径名为 pathname 的一个指定的文件;
type参数指定该I/O流的读、写方式,ISO C 规定可以有15种不同值。
2、读写字节
prototype:
#include <stdio.h>int fgetc(FILE *fp);int fputc(int c, FILE *fp);
3、读写行
prototype:
#include <stdio.h>char *fgets(char *restrict buf, int n, FILE *restrict fp);char *fputs(char *restrict buf, FILE *restrict fp);
4、定位流
prototype:
#include <stdio.h>int fseek(FILE *fp, long offset, int whence);
demo:
#include <apue.h>#include <stdio.h>#define LINESIZE 16 int main(int argc, const char *argv[]){ FILE *fsrc = fopen("test.c", "r"); if (!fsrc){ fprintf(stderr, "open source file error !"); exit(1); } FILE *fdst1 = fopen("1.c", "w"); if (!fdst1){ fprintf(stderr, "open destination file1 error !"); exit(1); } FILE *fdst2 = fopen("2.c", "w"); if (!fdst2){ fprintf(stderr, "open destination file2 error !"); exit(1); } /* 字节读写到文件 */ char c = fgetc(fsrc); while (c != EOF){ fputc(c, fdst1); //输出到标准输出端 c = fgetc(fsrc); //继续读取下一个字符 } fseek(fsrc, 0, SEEK_SET); //重新定位到文件头 /* 行读写到文件 */ char buf[LINESIZE]; while (fgets(buf, LINESIZE, fsrc) != NULL){ fputs(buf, fdst2); //输出文件 } fclose(fsrc); fclose(fdst1); fclose(fdst2); return 0;}
5、2进制I/O
prototype
ssize_t fread(void *buf, size_t size, size_t nobj, FILE *fp);ssize_t fwrite(const void *buf, size_t size, size_t nobj, FILE *fp);
返回实际读写的对象数。
注意 : 调用完 fwrite 后,必须用定位函数重新定位到文件头,才能读出数据,因为调用 fwrite 后,文件指针将指向文件尾。
6、格式化I/O
prototype
#include <stdio.h>int fscanf(FILE *fp, const char *format, ...);int fprintf(FILE *fp, const char *format, ...);
demo
#include <apue.h>#include <stdio.h>typedef struct student{ char name[20]; char agent[8]; double score;}stu;int main(int argc, const char *argv[]){ int n, i; printf("Please input class's number : "); scanf("%d",&n); stu class[n]; stu temp[n]; printf("*****************************\n"); printf("* *\n"); printf("* Please input student info *\n"); printf("* *\n"); printf("*****************************\n"); for (i = 0; i < n; i++){ printf("* %d *\n", i + 1); printf("name : "); fscanf(stdin, "%s", class[i].name ); printf("agent : "); fscanf(stdin, "%s", class[i].agent ); printf("score : "); fscanf(stdin, "%lf", &class[i].score); } FILE *fp = fopen("3.c", "r+"); fwrite(class, sizeof(stu), 2, fp); fseek(fp, 0, SEEK_SET); //重新定位到文件头,因为调用fwrite时,文件指针指向了文件尾 fread(temp, sizeof(stu), 2, fp); printf("******************************************************\n"); printf("* *\n"); printf("* Please check if there is any error of student info *\n"); printf("* *\n"); printf("******************************************************\n"); for (i = 0; i < n; i++){ fprintf(stdout, "* %d * \nname : %s \nagent : %s \nscore : %lf\n", i + 1, temp[i].name, temp[i].agent, temp[i].score); } fclose(fp); return 0;}
阅读全文
0 0
- 文件编程之C库函数
- 文件编程之库函数
- Linux C高级编程——文件操作之库函数
- 文件编程之库函数调用
- 文件编程之库函数方式
- Linux学习之库函数方式文件编程
- linux文件编程-C库函数使用以及时间编程
- 文件编程(库函数调用)
- 库函数方式文件编程
- 库函数文件编程练习
- 【Linux库函数编程】文件编程
- C语言编程常见问题解答之标准库函数(1)
- C语言编程常见问题解答之标准库函数(2)
- C库函数标准编程之fscanf()函数解读及其实验
- linux 中文件编程之C语言库函数
- Linux文件编程之--库函数pk系统调用函数
- Linux应用程序开发笔记->文件编程之库函数
- LS9-库函数方式文件编程之学习笔记
- MariaDB开启慢查询日志
- 域名忘记续费后的问题
- display:table-cell的集中应用
- 解决Perhaps you are running on a JRE rather than a JDK ?的问题
- CCF201403-3 命令行选项
- 文件编程之C库函数
- 字符串的排序及交换
- Ignite SQL网格快速学习(一)
- ue4 umg帧动画
- 基于注解的形式在Spring中实现对多数据源配置和使用
- 【PAT】【Advanced Level】1024. Palindromic Number (25)
- MyBatis的foreach语句详解
- linux-framebuffer设备相关参数
- docker搭建比特币私链