---restore content starts---
C library function file operation is independent of the specific system platform, the portability is good.
Library functions-Create and open
File*fopen (const char*filename,const Char*mode)
Mode: Open modes
Common open Mode:
R,RB: Read-only mode open
W,WB: Write-only open, if the file does not exist, create the file
A,ab: Append mode open, if file does not exist, create the file
r+,r+b,rb+: Read and write mode open
w+,w+b,wh+: Read and write mode open, if the file does not exist, create the file
a,a+b,ab+: Read and append mode open, if the file does not exist, create the file
B is used to differentiate between binary and text files, which is differentiated in Dos,window, where Linux does not differentiate between binary file box text files.
Library functions-Read
size_t fread (void*ptr,size_t size,size_t n,file *stream);
Reads n fields from a file pointed to by stream, each field is a size byte, and puts the read data into a character array referred to by PTR, returning the actual number of bytes read.
Library Functions-Write
size_t fwrite (void*ptr,size_t size,size_t n,file *stream);
Read characters:
int fgetc (file*stream);
---restore content ends---
C library function file operation is independent of the specific system platform, the portability is good.
Library functions-Create and open
File*fopen (const char*filename,const Char*mode)
Mode: Open modes
Common open Mode:
R,RB: Read-only mode open
W,WB: Write-only open, if the file does not exist, create the file
A,ab: Append mode open, if file does not exist, create the file
r+,r+b,rb+: Read and write mode open
w+,w+b,wh+: Read and write mode open, if the file does not exist, create the file
a,a+b,ab+: Read and append mode open, if the file does not exist, create the file
B is used to differentiate between binary and text files, which is differentiated in Dos,window, where Linux does not differentiate between binary file box text files.
Read
size_t fread (void*ptr,size_t size,size_t n,file *stream);
Write:
size_t fwrite (void*ptr,size_t size,size_t n,file *stream);
Read characters
int fgetc (file*stream);
Example:
#include <stdio.h>
Main ()
{
FILE*FP;
Char ch;
if ((Fp=fopen ("C1.txt", "RT")) ==null)
{
printf ("\ncannot open file strike any key exit");
Getch ();
Exit (1);
}
CH=FGETC (FP);
while (CH!=EOP)
{
Putchar (CH);
CH=FGETC (FP);
}
Fclose (FP);
}
Write character
int fputs (Inyt c,file*stream)
Example:
#include <stdio.h>
Main ()
{
FILE*FP;
Char ch;
if ((Fp=fopen ("string", "w+")) ==null)
{
printf ("\ncannot open file, strike any key exit");
Getch ();
Exit (1);
}
printf ("Input a string:\n");
Ch=fgetchar ();
while (ch!= ' \ n ')
{
FPUTC (CH,FP);
Ch=getchar ();
}
printf ("\ n");
Fclose (FP);
}
Format read:
FSCANF (File*stream,char*format[,argument ...])
Format input from a stream
#include <stdlib.h>
#include <stdio.h>
int mian (void)
{
int i;
printf ("Input an Interger:");
if (fscanf (stdin, "%d", &i))
printf ("The integer read was:%i\n", i);
}
Format write:
fprintf (File*stream,char*format[,argument ...])
Format the output from one stream
#include <stdlib.h>
#include <process.h>
File*stream;
int mian (void)
{
int i=10;
Double fp=1.5;
Char s[]= "This is a string";
Char c= ' \ n ';
Stream=fopen ("Fprintf.out", "" W);
fprintf (Stream, "%s%c", s,c);
fprintf (Stream, "%d\n", I);
fprintf (Stream, "%f\n", FP);
Fclose (stream);
}
Positioning:
int fseek (File*stream,long offset,int whence);
Whence:
Seek_set
Seek_cur
Seek_end
Path obtained:
CHAR*GETCWD (char*buffer,size_t size);
Copy the path to buffer, if buffer is too small, return-1;
To create a directory:
int mkdir (char*dir,int mode);
0 returns success, 1 indicates an error.
Linux application Development-file programming-Library functions