系统调用函数能够直接操作系统设备,C语言库函数是对系统调用函数的封装,增加了可移植性,
C语言库函数可以在各个系统上运行,而系统调用则会因为系统不同而有一定的差别。
在读写文件这个操作上,系统函数每次都会调用系统设备进行读写,但是C语言库函数则做了一定的优化,
C语言库函数提供了一个缓冲区,只有当缓冲区满了才会调用系统设备读写文件,保护了磁盘,减少系统消耗。
但是在读取键盘文件的方法上,系统调用使用的是read()函数,而C语言可以函数使用的是scanf(),fscanf()函数,
C语言的scanf()函数存在弊端(无法读取空格,不能按缓冲区大小读取),所以推荐使用系统调用。
打开和关闭文件
FILE * fopen(const char *path,const char *mode);
int fclose(FILE *stream)
fopen以mode模式打开名为path的文件
fopen返回一个文件指针
出现错误,fopen返回NULL,并把errno设置为恰当的值 mode模式说明
"r":以读的方式打开文件,文件流的位置在文件的开始。
"r+":以读写的方式打开文件,文件流的位置在文件的开始。
"w":如果文件存在,截断这个文件,让文件的大小变为0,文件流的位置在文件的开始。
"w+":以读写的方式打开文件,如果文件不存在,建立该文件,如果文件存在就截断文件,文件流的位置在文件的开始。
"a":以追加的方式打开文件,在文件的末尾开始写,如果文件不存在,创建该文件,文件流的位置在文件的末尾。
"a+":以读写的方式打开文件(如果是写文件,在文件的末尾开始写),如果文件不存在,创建该文件;从文件头开始读文件,但是在文件尾部追加文件。
备注:在Linux系统环境下,读写二进制文件或者文本文件只有这6种mode模式,
但是在windows系统环境下,读写文本文件是这6种模式,读写二进制文件则必须要加上'b',例如"rb"--读二进制文件
size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream)
size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream)
参数ptr指向缓冲区保存或读取的数据
参数size表示单个记录大小
参数nmemb最大允许读取或回写的记录数
函数返回值是已经读取或回写的记录数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> typedef struct _person
{
int num;
char name[];
int age;
} Person; int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in write mode
pf = fopen(args[], "w");
//judge file pointer
if (pf == NULL)
{
/*
注意:这里和系统函数read有所区别,系统read函数返回的是int型文件标识符
而C语言库函数返回的是FILE结构体指针
系统read函数判断文件标识符是否是-1
C语言库函数fread判断返回的文件指针是否为NULL
*/
printf("error msg:%s\n", strerror(errno));
return ;
}
/*
fread()和fwrite()一般用于二进制文件的读写
*/ //size_t类型相当于unsigned int(无符号整型)
size_t rc=;
//define person object
//Initialization
Person arr[];
memset(&arr, , sizeof(arr));
arr[].num = ;
strcpy(arr[].name, "ming");
arr[].age = ; arr[].num = ;
strcpy(arr[].name, "hong");
arr[].age = ; arr[].num = ;
strcpy(arr[].name, "fei");
arr[].age = ; rc= fwrite(arr,sizeof(Person),,pf);
//print record count
printf("record count:%d\n",rc);
//close file pointer
fclose(pf);
return ;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> typedef struct _person
{
int num;
char name[];
int age;
} Person; int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in read mode
pf = fopen(args[], "r");
//judge file pointer
if (pf == NULL)
{ printf("error msg:%s\n", strerror(errno));
return ;
}
size_t rc=;
int i=;
Person arr[];
memset(&arr,,sizeof(arr));
rc= fread(arr,sizeof(Person),,pf);
//print record count
printf("record count:%d\n",rc);
for(;i<;i++)
{
printf("num=%d\n",arr[i].num);
printf("name=%s\n",arr[i].name);
printf("age=%d\n",arr[i].age);
}
//close file pointer
fclose(pf);
return ;
}
格式化输入和输出调用
int fprintf(FILE * stream,const char *format,...)
int fscanf(FILE * stream,const char *format,...)
//fprintf()函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in read mode
pf = fopen(args[], "w");
//judge file pointer
if (pf == NULL)
{ printf("error msg:%s\n", strerror(errno));
return ;
}
char buf[]={};
int i=;
strcpy(buf,"fly on air");
for(;i<;i++)
{
fprintf(pf,"%s--num=%d\n",buf,i);
}
//close file pointer
fclose(pf);
return ;
}
//fscanf()函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in read mode
pf = fopen(args[], "r");
//judge file pointer
if (pf == NULL)
{ printf("error msg:%s\n", strerror(errno));
return ;
}
char buf[]={};
//EOF是一个常量,表示文件结尾
/*
scanf(),fscanf()两大缺点
缺点一:遇到空格或者'\n'结束当前读取
缺点二:不会考虑缓冲区的大小,读取的内容很可能超出缓冲区大小,导致报错
*/
while(fscanf(pf,"%s",buf)!=EOF)
{
printf("%s\n",buf);
}
//close file pointer
fclose(pf);
return ;
}
行输入和输出调用。
char *fgets(char *s, int size, FILE *stream);
int fputc(char *s, FILE *stream);
fgets从文件中读取一行,返回EOF代表文件结尾。
fputs向文件中写入一行
//fputs()函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in write mode
pf = fopen(args[], "w");
//judge file pointer
if (pf == NULL)
{ printf("error msg:%s\n", strerror(errno));
return ;
}
char buf[]={};
sprintf(buf,"fly with me\n");
fputs(buf,pf);
//close file pointer
fclose(pf);
return ;
}
//fgets()函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> int main(int arg, char * args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return ;
}
//open the file
FILE * pf = NULL;
//open the file in read mode
pf = fopen(args[], "r");
//judge file pointer
if (pf == NULL)
{ printf("error msg:%s\n", strerror(errno));
return ;
}
char buf[]={};
/*
fgets()函数读到文件结尾会返回NULL,不是EOF */
while(fgets(buf,sizeof(buf),pf)!=NULL)
{
/*
fgets()会读出每一行的换行符'\n',所以读出来的数据不要加'\n'
fscanf()不会读出'\n'
*/
printf("%s",buf);
} //close file pointer
fclose(pf);
return ;
}

最新文章

  1. 一个简单的消息提示jquery插件
  2. dfs判断连通图(无向)
  3. Java 的设计模式之一装饰者模式
  4. WPF界面布局——各种控件
  5. 无忧之道:Docker中容器的备份、恢复和迁移
  6. Sqli-LABS通关笔录-11[sql注入之万能密码以及登录框报错注入]
  7. mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)” 解决办法
  8. Python之数据序列化(json、pickle、shelve)
  9. php引用传值详解
  10. FTP上传文件,报错java.net.SocketException: Software caused connection abort: recv failed
  11. Jsoup的使用
  12. CenterOS7.5中搭建wordpress
  13. webpack详细配置解析
  14. [日常工作]Win2008r2 以及更高版本的操作系统安装Oracle10.2.0.5
  15. vue原理探索--响应式系统
  16. 【做题】51NOD1518 稳定多米诺覆盖——容斥&amp;dp
  17. 11-01 Java 开发工具 eclipse从下载、安装到实际使用的详细教程
  18. Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
  19. MySQL笔记(三)由txt文件导入数据
  20. Xilinx问题查找

热门文章

  1. Automatic Diagnostic Repository
  2. GLEW扩展库【转】
  3. ActiveMQ Spring 集成配置
  4. socket编程演示样例(多线程)
  5. IDEA+MAVEN+testNG(reportNG)
  6. RGB颜色工具大全 and 网页配色方案
  7. 用unity3d切割图片
  8. java自定义注解与反射
  9. IOS 代理模式 DELEGATE
  10. lucene: nDocs must be &gt; 0查询异常解决