1. 程式人生 > >Linux C,文件讀寫函數

Linux C,文件讀寫函數

err urn view stdlib.h ungetc put stream end linu

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
View Code

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 }
View Code

Linux C,文件讀寫函數