Linux C, file read and write functions

Source: Internet
Author: User
Tags fread function prototype

The C standard library provides a very large number of functions for reading and writing files, and most functions are declared in stdio.h.

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

These function prototype declarations are all in stdio.h, as follows:

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);

Whether you are writing to a file or reading from a stream of files, you must first open the file, and then close the open file when you are finished.

To prevent the pointer from becoming a wild pointer, you should also point the file pointer to 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);

The security version of the fopen function is fopen_s (FILE *stream,char *filename,char *mode), which is used before the macro

Fileutil.h

1 #ifndef __fileutil_h2 3 #define__fileutil_h4 5#include <stdio.h>6 7FILE *open_file (Const Char*file,Const Char*mode);8 9 voidREAD0 (Const Char*file);Ten  One voidRead1 (Const Char*file); A  - voidRead2 (Const Char*file); -  the voidWrite1 (Const Char*file); -  - #endif
View Code

Fileutil.c

1 /*2 3  * =====================================================================================4 5 * FILENAME:FILEUTIL.C6 7 * Description:8 9 * version:1.0Ten  One * created:2017 April 13 09:38 23 seconds A  - * Revision:none -  the * COMPILER:GCC -  - * Author:your NAME (), -  + * Organization: -  +  * ===================================================================================== A  at  */ -  -#include <stdio.h> -  -#include <stdlib.h> -  in#include <string.h> -  to#include"fileutil.h" +  -   the  *FILE * $ Panax NotoginsengOpen_file (Const Char*file,Const Char*mode) -  the { +  AFILE *FP; the  +  if(! (fp =fopen (File,mode))) { -  $Perror ("Open File Error"); $  -Exit (-1); -  the  } - Wuyi  returnFP; the  - } Wu  -   About  $ voidREAD0 (Const Char*file) -  - { -  AFile *FP = open_file (file,"R"); +  the  CharBuf[bufsiz] = {0}; -  $UnsignedLongt =0; the  the  //int tmp = fread (BUF,1,20,FP); the  the  //printf ("Read%d bytes\n", TMP); -  in  //printf ("Read buf from%s is%s\n", file,buf); the  the   while(t = fread (BUF,1,192, fp))! =0){ About  theprintf"%s\n", buf); the  theBzero (&buf,sizeof(BUF)); +  -  } the Bayi  if(fclose (fp)! =0) Perror ("Close File Error"); the  the } -  -   the  the voidRead1 (Const Char*file) the  the { -  theFile *FP = open_file (file,"R"); the  the  Char*buf;94  thesize_t n =0; the  the   while((n = getline (&AMP;BUF,&AMP;N,FP))! = (size_t)-1){98  Aboutprintf"%s", buf); - 101Bzero (BUF,sizeof(BUF));102 103  }104  the  //if (buf) free (BUF);106 107  if(fclose (fp)! =0) Perror ("Close File Error");108 109 }  the 111   the 113 voidRead2 (Const Char*file) the  the { the 117File *FP = open_file (file,"R");118 119  CharBuf[bufsiz] =""; - 121   while(Fgets (BUF,BUFSIZ,FP)) {122 123printf"%s", buf);124  the bzero (buf,bufsiz);126 127  } - 129  if(fclose (fp)! =0) Perror ("Close File Error"); the 131 } the 133  134 135 /*not yet implemented*/136 137 voidWrite1 (Const Char*file)138 139 { $ 141File *FP = open_file (file,"a+t");142 143  if(fclose (fp)! =0) Perror ("Close File Error");144 145}
View Code

Linux C, file read and write functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.