Linux C,文件读写函数

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

 1 #ifndef __FILEUTIL_H
 2
 3 #define __FILEUTIL_H
 4
 5 #include <stdio.h>
 6
 7 FILE *open_file(const char *file,const char *mode);
 8
 9 void read0(const char *file);
10
11 void read1(const char *file);
12
13 void read2(const char *file);
14
15 void write1(const char *file);
16
17 #endif

fileutil.c

  1 /*
  2
  3  * =====================================================================================
  4
  5  *       Filename:  fileutil.c
  6
  7  *    Description:
  8
  9  *        Version:  1.0
 10
 11  *        Created:  2017年04月13日 09时38分23秒
 12
 13  *       Revision:  none
 14
 15  *       Compiler:  gcc
 16
 17  *         Author:  YOUR NAME (),
 18
 19  *   Organization:
 20
 21  * =====================================================================================
 22
 23  */
 24
 25 #include <stdio.h>
 26
 27 #include <stdlib.h>
 28
 29 #include <string.h>
 30
 31 #include "fileutil.h"
 32
 33
 34
 35 FILE *
 36
 37 open_file(const char *file,const char *mode)
 38
 39 {
 40
 41  FILE *fp;
 42
 43  if(!(fp = fopen(file,mode))){
 44
 45  perror("open file error");
 46
 47  exit(-1);
 48
 49  }
 50
 51  return fp;
 52
 53 }
 54
 55
 56
 57 void read0(const char *file)
 58
 59 {
 60
 61  FILE *fp = open_file(file,"r");
 62
 63  char buf[BUFSIZ] = {0};
 64
 65  unsigned long t = 0;
 66
 67  //int tmp = fread(buf,1,20,fp);
 68
 69  //printf("read %d bytes\n",tmp);
 70
 71  //printf("read buf from %s is %s\n",file,buf);
 72
 73  while((t = fread(buf,1,192,fp)) != 0){
 74
 75  printf("%s\n",buf);
 76
 77  bzero(&buf,sizeof(buf));
 78
 79  }
 80
 81  if(fclose(fp) != 0) perror("close file error");
 82
 83 }
 84
 85
 86
 87 void read1(const char *file)
 88
 89 {
 90
 91  FILE *fp = open_file(file,"r");
 92
 93  char *buf;
 94
 95  size_t n = 0;
 96
 97  while((n = getline(&buf,&n,fp)) != (size_t)-1){
 98
 99  printf("%s",buf);
100
101  bzero(buf,sizeof(buf));
102
103  }
104
105  //if(buf) free(buf);
106
107  if(fclose(fp) != 0) perror("close file error");
108
109 }
110
111
112
113 void read2(const char *file)
114
115 {
116
117  FILE *fp = open_file(file,"r");
118
119  char buf[BUFSIZ] = "";
120
121  while(fgets(buf,BUFSIZ,fp)){
122
123  printf("%s",buf);
124
125  bzero(buf,BUFSIZ);
126
127  }
128
129  if(fclose(fp) != 0) perror("close file error");
130
131 }
132
133
134
135 /* 尚未实现 */
136
137 void write1(const char *file)
138
139 {
140
141  FILE *fp = open_file(file,"a+t");
142
143  if(fclose(fp) != 0) perror("close file error");
144
145 }

时间: 2024-08-26 14:53:42

Linux C,文件读写函数的相关文章

(转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind

http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/8776479 在C\C++中,文件操作都是由库函数来实现的,主要是分为读和写两种操作,以下详细讲解以下所有有关文件操作的用法: (1)fopen()函数:打开文件 包含头文件:#include 格式:FILE * fopen(const char * path,const char * mode)

C文件读写函数介绍(转)

1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此流相连接,给此流返回一个FILR指针. 参数filename指向要打开的文件名,mode表示打开状态的字符串,其取值如下: 字符串 含义 "r" 以只读方式打开文件 "w" 以只写方式打开文件 "a" 以追加方式打开文件 "r+"

Linux Direct 文件读写(文件DIO)

有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法: (1)打开文件时,添加O_DIRECT参数: 需要定义_GNU_SOURCE,否则找不到O_DIRECT宏定义 示例片段: #define _GNU_SOURCE #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int fd = open("test.out"

文件读写函数

字符读写函数:    fgetc / fputc字符串读写函数:fputs / fgets格式化读写函数:fscanf / fprintf二进制读写函数:fread / fwrite其他相关函数:检测文件结尾函数feof检测文件读写出错函数ferror清除末尾标志和出错标志函数clearerr文件定位的函数fseek 字符读写函数fgetc和fputc while( !feof( fp1 ) ){ c = fgetc( fp1 ); fputc(c, fp2); } 函数fputc( )    

封装 INI 文件读写函数

delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗体创建的时候,读取ini文件信息 procedure TfrmAFN04H_F9.FormCreate(Sender: TObject); var vFIni: TIniFile; sFileName: string; begin sFileName := ExtractFileDir(Applic

Python-Day3 Python基础进阶之集和/文件读写/函数

一.集和 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 1.创建集合 >>> s = set([3,5,9,10]) #创建一个数值集合 >>> t = set("Hello") #创建一个唯一字符的集合 >>> s {9, 10, 3, 5} >>> t {'e', 'H', 'l', 'o'} #集和自动去重

Linux设置文件读写权限

设置文件夹的读写权限: sudo chmod -R 777 /data 权限码描述 sudo chmod 600 ××× (只有所有者有读和写的权限)sudo chmod 644 ××× (所有者有读和写的权限,组用户只有读的权限)sudo chmod 700 ××× (只有所有者有读和写以及执行的权限)sudo chmod 666 ××× (每个人都有读和写的权限)sudo chmod 777 ××× (每个人都有读和写以及执行的权限) -R表示包含设置所有子目录

fread和fwrite同时对一个文件读写

这几天看到了fopen的参数设置.中文的那些真的是不能帮助精确理解.在网上发现了英文的,特附上: FILE *fopen(const char *filename, const char *mode)fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:"r"open text file for reading"

通过文件读写方式实现Matlab和Modelsim的联合仿真

虽然Modelsim的功能非常强大,仿真的波形可以以多种形式进行显示,但是当涉及到数字信号处理的算法的仿真验证的时候,则显得有点不足.而进行数字信号处理是Matlab的强项,不但有大量的关于数字信号处理的函数,而且图形显示功能也很强大,所以在做数字信号处理算法的FPGA验证的时候借助Matlab会大大加快算法验证的速度. 关于Matlab和Modelsim联合仿真,我从网上看到两种方法,一种是通过Link for Modelsim建立Matlab和Modelsim的联合仿真接口:另一种就是通过文