学会用LinuxC文件读写函数

C标准库提供的用于读写文件的函数非常多,大多数函数都在stdio.h中声明。

fread/fwri     te   ,fgets/fputs,fgetchar/fputchar,fprintf/fs     can   f.。..。..。..。..

这些函数原型声明都在stdio.h中,如下:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

int fgetc(FILE *stream);

char *fgets(char *s, int size, FILE *stream);

int getc(FILE *stream);

int getchar(void);

int ungetc(int c, FILE *stream);

无论是写入文件还是从文件流流中读取,都要先打开文件,完成后还要将打开的文件关闭。

为了防止指针变成野指针,还应将文件指针指向NULL。

FILE *fopen(const char *pathname, const char *mode);

FILE *fdopen(int fd, const char *mode);

FILE *freopen(const char *pathname, const char *mode, FILE *stream);

fopen函数的安全版本是fopen_s(FILE *stream,char *filename,char *mode),使用之前要将宏

fileu     ti   l.h

#ifndef __FILEU  TI L_H

#define __FILEU  TI L_H

#include

FILE *open_file(const char *file,const char *mode);

void read0(const char *file);

void read1(const char *file);

void read2(const char *file);

void write1(const char *file);

#endif

fileu  TI l.c

/*

* =====================================================================================

* Filename: fileu  TI l.c

* Description:

* Ve     rs   ion: 1.0

* Created: 2017年04月13日 09时38分23秒

* Revision: none

* Com     pi   ler: gcc

* Author: YOUR NAME (),

* Organization:

* =====================================================================================

*/

#include

#include

#include

#include “fileutil.h”

FILE *

open_file(const char *file,const char *mode)

{

FILE *fp;

if(!(fp = fopen(file,mode))){

perror(“open file error”);

exit(-1);

}

return fp;

}

void read0(const char *file)

{

FILE *fp = open_file(file,“r”);

char buf[BUFSIZ] = {0};

unsigned long t = 0;

//int tmp = fread(buf,1,20,fp);

//printf(“read %d bytes\n”,tmp);

//printf(“read buf f     rom   %s is %s\n”,file,buf);

while((t = fread(buf,1,192,fp)) != 0){

printf(“%s\n”,buf);

bzero(&buf,sizeof(buf));

}

if(fclose(fp) != 0) perror(“close file error”);

}

void read1(const char *file)

{

FILE *fp = open_file(file,“r”);

char *buf;

size_t n = 0;

while((n = getline(&buf,&n,fp)) != (size_t)-1){

printf(“%s”,buf);

bzero(buf,sizeof(buf));

}

//if(buf) free(buf);

if(fclose(fp) != 0) perror(“close file error”);

}

void read2(const char *file)

{

FILE *fp = open_file(file,“r”);

char buf[BUFSIZ] = “”;

while(fgets(buf,BUFSIZ,fp)){

printf(“%s”,buf);

bzero(buf,BUFSIZ);

}

if(fclose(fp) != 0) perror(“close file error”);

}

/* 尚未实现 */

void write1(const char *file)

{

FILE *fp = open_file(file,“a+t”);

if(fclose(fp) != 0) perror(“close file error”);

}

学会用LinuxC文件读写函数_设计制作_RF/无线
47
143
0
69

相关资讯

  1. 1、多国奥斯卡外语片已选定印度新人作爆冷引热议3795
  2. 2、喜剧片《独行月球》杀青!沈腾马丽主演,这次“含腾量”太高了1536
  3. 3、《别想打扰我学习》28岁的半熟少女,职场精英,李兰迪演得怎样?4419
  4. 4、《小魔仙2》今日公映五大看点圆小朋友魔仙梦2371
  5. 5、听说“速激”系列也要开启宇宙了?“郭达森”片场照揭秘冰山一角2478
  6. 6、关晓彤缺课太多留级成王俊凯同学?知情人辟谣否认留级150
  7. 7、《白日》强硬背后:电影局、中影、华夏齐护航169
  8. 8、赵本山推自传剧《不是钱的事》颠覆出演成亮点695
  9. 9、华谊兄弟压力大了?沈腾新片获吴京盛赞:票房至少60亿3989
  10. 10、双女主剧vs双男主剧,谁的武力值更强?3294
全部评论(0)
我也有话说
0
收藏
点赞
顶部