手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux C文件读写函数

[日期:2017-05-08] 来源:Linux社区  作者:szyk [字体: ]

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

fread/fwrite,fgets/fputs,fgetchar/fputchar,fprintf/fscanf.............

这些函数原型声明都在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),使用之前要将宏

fileutil.h

#ifndef __FILEUTIL_H
#define __FILEUTIL_H
#include <stdio.h>
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

fileutil.c

/*

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

 *      Filename:  fileutil.c

 *    Description: 

 *        Version:  1.0

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

 *      Revision:  none

 *      Compiler:  gcc

 *        Author:  YOUR NAME (),

 *  Organization: 

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

 */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#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 from %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");

}

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-05/143553.htm

linux
相关资讯       Linux c 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款