系统调用函数能够直接操作系统设备,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 ;
}

Linux 文件管理(C语言库函数一)的更多相关文章

  1. Linux基础与Linux下C语言编程基础

    Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...

  2. 【转】Linux基础与Linux下C语言编程基础

    原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...

  3. linux 下C语言学习路线

    UNIX/Linux下C语言的学习路线.一.工具篇“公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工 ...

  4. Linux下C语言编程实现spwd函数

    Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...

  5. Linux文件管理相关命令

    Linux文件管理相关命令   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...

  6. LINUX下C语言编程基础

    实验二 Linux下C语言编程基础 一.实验目的 1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用 ...

  7. 如何为Linux安装Go语言

    导读 Go 语言又称为 golang, 是由 Google 最初开发的一种开源编程语言,其在设计时就遵循了简单.安全和速度的 3 大原则.Go 语言具有多种调试.测试.分析和代码审查工具,如今 Go ...

  8. C语言库函数--操作文件

    //C库函数读取文件的代码 I/O缓冲机制 C语言库函数写文件都是写在内存中,然后一次写入磁盘.提高了效率. 读写文件,不对系统进行操作,一般采用C语言库函数.移植可以在任何可以对C支持的操作系统,而 ...

  9. LINUX下中文语言包的安装(转)

    在安装盘上已经有各种语言包了,我们只需要找到他们,并安装就可以了.中文的是fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0- ...

随机推荐

  1. ldr与adr的区别

    参考: http://coon.blogbus.com/logs/2738861.html http://hi.baidu.com/for_guanghui/item/73e60bbcc8be15a2 ...

  2. C#的Xamarin开发小米盒子应用并以WCF实现微信通知

    对于熟悉C#语言的开发人员而言,用Xamarin开发Android应用也是一个不错的选择.小米盒子是Android系统.当然也就能够使用Xamarin来开发.首选来看效果图. watermark/2/ ...

  3. django admin中文输入编码错误

    修改models里面的str方法,改为unicode class Category(models.Model): name = models.CharField(max_length=20, verb ...

  4. window7访问虚拟机ubuntu中的mysql

    window7上面下载mysql很麻烦,不喜欢,所以改用虚拟机安装ubuntu系统,提供mysql服务. 第一步:下载vmware workstation12, 第二步:下载ubuntu镜像,我用的是 ...

  5. [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)

    Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...

  6. Amixer 控制声音

    amixer set Master XXXX 就可以直接控制主声卡属性 amixer set Master 20 #设置主声卡声音为 20 amixer set Master off #关闭主声卡(静 ...

  7. 打造你爱不释手的编辑器sublime3

    首先去官网下载你的sublime3 让后安装好package control 去package control官网 安装好package control 安装emmet,和格式化工具 接着安装一个好主 ...

  8. PHP中单引号双引号使用原则

    PHP中单引号双引号使用原则   1.PHP中尽量用单引号,HTML代码全部用双引号   2.在包含变量的时候,用双引号可以简化操作   3.复杂的情况下用大括号包起来   4 PHP引号还有一个用处 ...

  9. CSS/JavaScript hacks,browserhacks使用

    1.网址 http://browserhacks.com/ 2.使用 (1)JavaScript Hacks 浏览器js判断 (2)条件注释hack (3)Media Query Hacks 媒体查询 ...

  10. Maven入门学习

    1 Maven的安装 maven下载路径:http://maven.apache.org/download.cgi 我是在win7上安装了,安装后在cmd输入 mvn -v: C:\Users\*** ...