C language file Operation function Daquan (super detail) _c language

Source: Internet
Author: User
Tags define function fread function prototype int size rewind stdin string format
fopen (Open file)
Correlation function Open,fclose
Table header file #include <stdio.h>
Defines the function FILE * fopen (const char * path,const char * mode);
The function description parameter path string contains the file path and filename to open, and the parameter mode string represents the flow pattern.
mode has the following morphological strings:
R to open a read-only file, the file must exist.
r+ open a file that can be read and write, the file must exist.
W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.
w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.
A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained.
A + opens the writable file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained.
 code as follows:

R Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
W Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ Open for reading and writing.  The file is created if it does isn't exist otherwise it is truncated. The stream is posi‐
Tioned at the beginning of the file.
A Open for appending (writing in end of file).  The file is created if it does not exist. The stream is positioned at the
End of the file.
A + Open for reading and appending (writing in end of file).  The file is created if it does not exist. The initial file posi‐
tion for reading are at the beginning of the file, but output is always appended to the "end of" the file.

The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file. However, in the POSIX system, including Linux ignores the character. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.
When the return value file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code exists in errno.
Additional instructions Generally speaking, the file will be read or written after the action, if the file failure, the next reading and writing can not be smoothly, so in fopen () after the error judgment and processing.
Example
code as follows:

#include <stdio.h>
Main ()
{
FILE * FP;
Fp=fopen ("Noexist", "A +");
if (fp= =null) return;
Fclose (FP);
}

1. fprintf
Function: Transfer formatted output to a file
Table header file: #include <stdio.h>
function prototypes: int fprintf (FILE *stream, char *format[, argument,...]);
file* A pointer to a file type
char* format the input function in the same format as printf
Return value: Returns the number of bytes converted when successful, and returns a negative when failed
fp = fopen ("/local/test.c", "A +");
fprintf (FP, "%s\n", str);

2. fscanf
Function: Perform formatted input from one stream
Table header file: #include <stdio.h>
Function prototype: int fscanf (FILE *stream, char *format[,argument ...]);
file* A pointer to a file type
char* formatted output function, as in the scanf format
Return value: Returns the number of bytes converted when successful, and returns a negative when failed
fp = fopen ("/local/test.c", "A +");
FSCANF (FP, "%s", str);

3. clearerr (clear file stream error flag)
Correlation function feof
Table header file #include <stdio.h>
Defines the function void Clearerr (FILE * stream);
The function Description Clearerr () Clears the error flag used by the file stream specified by the parameter stream.
return value

4.fclose (Close file)
Correlation function Close,fflush,fopen,setbuf
Table header file #include <stdio.h>
define function int fclose (FILE * stream);
The function Description fclose () is used to close the previously fopen () Open file. This action will cause the data in the buffer to be written to the file and release the file resources provided by the system.
The return value returns 0 if the closed file action succeeds, and returns EOF when an error occurs and saves the error code to errno.
The error code EBADF represents a file that is not open for the parameter stream.
Please refer to fopen () for examples.

5.fdopen (convert file descriptor to file pointer)
Correlation function Fopen,open,fclose
Table header file #include <stdio.h>
Defines the function FILE * fdopen (int fildes,const char * mode);
The function Description Fdopen () Converts the file descriptor of the parameter fildes to the corresponding file pointer and returns. The argument mode string represents the flow pattern of the file pointer, which must be the same as the original file description Word read and write mode. Refer to fopen () for the mode string format.
Returns a file pointer to the stream when the return value conversion succeeds. Failure returns null and the error code exists in errno.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * FP =fdopen (0, "w+");
fprintf (FP, "%s/n", "hello!");
Fclose (FP);
}
Execute hello!

6.feof (check that the file stream is read to the end of the file)
Correlation function Fopen,fgetc,fgets,fread
Table header file #include <stdio.h>
define function int feof (FILE * stream);
Function Description feof () is used to detect whether the end of the file is read, and the mantissa stream is the file pointer returned by fopen (). Returns a value other than 0 if it is at the end of the file, and returns 0.
The return value returns a value other than 0 that represents the end of the file that has reached.

7.fflush (update buffer)
Correlation function Write,fopen,fclose,setbuf
Table header file #include <stdio.h>
define function int Fflush (file* stream);
The function Description Fflush () forces the data in the buffer to be written back into the file specified by the parameter stream. If the parameter stream is Null,fflush (), all open file data is updated.
The return value returns 0 successfully, the failure returns EOF, and the error code is stored in the errno.
Error code EBADF parameter stream the file specified is not open, or the state is open read-only. Other error code reference write ().

8.fgetc (read one character from the file)
Correlation function Open,fread,fscanf,getc
Table header File include<stdio.h>
Defines the function NT fgetc (FILE * stream);
The function Description fgetc () reads a character from the file that the parameter stream refers to. Returns EOF if the end of the file is read and no data is returned.
The return value GETC () returns the character read, and if EOF is returned to the end of the file.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE *FP;
int C;
Fp=fopen ("exist", "R");
while ((C=FGETC (FP))!=eof)
printf ("%c", c);
Fclose (FP);
}

9.fgets (read a string from the file)
Correlation function Open,fread,fscanf,getc
Table header File include<stdio.h>
Define Function Har * fgets (char * s,int size,file * stream);
The function Description fgets () is used for reading characters from the file referred to in the parameter stream to the memory space referred to by parameter S, until the newline character is read, the end of the file or the size-1 character has been read, and the end of the string is added with null.
return value gets () returns the S pointer if successful, and returns null to indicate an error occurred.
Example
 code as follows:

#include <stdio.h>
Main ()
{
Char s[80];
Fputs (Fgets (S,80,stdin), stdout);
}
Execute this is a test/* input/*
This is a test/* output * *

10.fileno (returns the file description used by the file stream)
Correlation function Open,fopen
Table header file #include <stdio.h>
define function int Fileno (FILE * stream);
The function Description Fileno () is used to get the file descriptor used by the file stream specified by the parameter stream.
The return value returns the file descriptor.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * FP;
int FD;
Fp=fopen ("/etc/passwd", "R");
Fd=fileno (FP);
printf ("fd=%d/n", FD);
Fclose (FP);
}
Execute fd=3

12.FPUTC (writes a specified character to the file stream)
Correlation function FOPEN,FWRITE,FSCANF,PUTC
Table header file #include <stdio.h>
Defines the function int FPUTC (int c,file * stream);
Function Description FPUTC converts parameter c to unsigned char and writes to the file specified by the parameter stream.
The return value FPUTC () returns the character written successfully, that is, parameter C. If EOF is returned, a write failure is represented.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * FP;
Char a[26]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
fp= fopen ("Noexist", "w");
for (i=0;i<26;i++)
FPUTC (A,FP);
Fclose (FP);
}

13.fputs (writes a specified string to the file)
Correlation function FOPEN,FWRITE,FSCANF,FPUTC,PUTC
Table header file #include <stdio.h>
Defines the function int fputs (const char * s,file * stream);
The function Description fputs () is used to write the string that the parameter s refers to in the file referred to by the parameter stream.
The return value returns the number of characters written if successful, and the return of EOF indicates an error occurred.
please refer to fgets () for examples.
Fread (read data from file stream)
Correlation function fopen,fwrite,fseek,fscanf
Table header file #include <stdio.h>
Defines functions size_t fread (void * ptr,size_t size,size_t nmemb,file * stream);
The function Description Fread () is used to read data from the file stream. The parameter stream is an open file pointer, and the parameter PTR points to the data space to be read in, and the number of characters read is determined by the parameter size*nmemb. Fread () returns the number of Nmemb actually read, if the value is smaller than the parameter nmemb, the representative may have read the end of the file or an error occurred, and then you must use Feof () or ferror () to determine what happened.
The return value returns the number of Nmemb actually read.
Additional Instructions
Example
 code as follows:

#include <stdio.h>
#define NMEMB 3
struct test
{
Char name[20];
int size;
}S[NMEMB];
int main () {
FILE * stream;
int i;
stream = fopen ("/tmp/fwrite", "R");
Fread (s,sizeof (struct test), nmemb,stream);
Fclose (stream);
for (i=0;i<nmemb;i++)
printf ("name[%d]=%-20s:size[%d]=%d/n", i,s.name,i,s.size);
}
Perform
name[0]=linux! Size[0]=6
name[1]=freebsd! Size[1]=8
name[2]=windows2000 size[2]=11

14.freopen (open file)
Correlation function Fopen,fclose
Table header file #include <stdio.h>
Define function FILE * freopen (const char * path,const char * mode,file * stream);
Function description parameter path string contains the file path and filename you want to open, parameter mode refer to the fopen () description. The parameter stream is an open file pointer. Freopen () closes the file stream that was opened by the original stream, and then opens the file for the parameter path.
When the return value file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code is in errno.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * FP;
Fp=fopen ("/etc/passwd", "R");
Fp=freopen ("/etc/group", "R", FP);
Fclose (FP);
}

15.fseek (move file stream to read/write location)
Correlation function Rewind,ftell,fgetpos,fsetpos,lseek
Table header file #include <stdio.h>
define function int fseek (FILE * stream,long offset,int whence);
Function Description Fseek () is used to move the file stream's read-write location. The parameter stream is an open file pointer, and the parameter offset is the number of displacements that move the read-write position based on the parameter whence.
The parameter whence is one of the following:
Seek_set the offset displacement from the beginning of the file to the new read-write location. The seek_cur increases the offset by the current reading and writing position.
Seek_end the read and write position to the end of the file and then increases the offset amount.
When the whence value is Seek_cur or seek_end, parameter offset allows negative values to occur.
The following are more specific ways to use:
1 To move the read-write location to the beginning of the file: fseek (file *stream,0,seek_set);
2 to move the read-write location to the end of the file: fseek (file *stream,0,0seek_end);
Returns 0 when the call succeeds, and returns -1,errno if there is an error, which holds the error code.
Additional Instructions fseek () do not return a read-write location like Lseek (), so you must use Ftell () to get the current read-write location.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * stream;
Long offset;
fpos_t POS;
Stream=fopen ("/etc/passwd", "R");
Fseek (Stream,5,seek_set);
printf ("offset=%d/n", Ftell (stream));
Rewind (stream);
Fgetpos (Stream,&pos);
printf ("offset=%d/n", POS);
pos=10;
Fsetpos (Stream,&pos);
printf ("offset =%d/n", Ftell (stream));
Fclose (stream);
}
Perform offset = 5
Offset =0
offset=10

16.ftell (Get the read location of the file stream)
Correlation function Fseek,rewind,fgetpos,fsetpos
Table header file #include <stdio.h>
Definition function Long ftell (FILE * stream);
The function Description Ftell () is used to obtain the current read/write location of the file stream. The parameter stream is an open file pointer.
Returns the current read-write position when the invocation succeeds, and returns -1,errno if there is an error.
Error code EBADF The stream of files for which the parameter stream is invalid or can be moved to read/write location.
Example Reference fseek ().

17.fwrite (write data to file stream)
Correlation function fopen,fread,fseek,fscanf
Table header file #include <stdio.h>
Define function size_t fwrite (const void * ptr,size_t size,size_t nmemb,file * stream);
The function Description fwrite () is used to write data to the file stream. The parameter stream is an open file pointer, and the parameter PTR points to the data address to write, and the total number of characters written is determined by the parameter size*nmemb. Fwrite () returns the number of Nmemb actually written.
The return value returns the number of Nmemb actually written.
Example
 code as follows:

#include <stdio.h>
#define SET_S (x,y) {Strcoy (s[x].name,y); S[x].size=strlen (y);}
#define NMEMB 3
struct test
{
Char name[20];
int size;
}S[NMEMB];
Main ()
{
FILE * stream;
set_s (0, "linux!");
set_s (1, "freebsd!");
set_s (2, "Windows2000.");
Stream=fopen ("/tmp/fwrite", "w");
Fwrite (s,sizeof (struct test), nmemb,stream);
Fclose (stream);
}
Execution reference fread ().

18.getc (read one character from the file)
Correlation function Read,fopen,fread,fgetc
Table header file #include <stdio.h>
define function int getc (FILE * stream);
The function Description getc () is used to read a character from the file that the parameter stream refers to. Returns EOF if the end of the file is read and no data is returned. Although Getc () is the same as fgetc (), getc () is defined as a macro and is not a real function call.
The return value GETC () returns the character read, and if EOF is returned to the end of the file.
Example reference fgetc ().

19.getchar (read in one character from standard input device)
Correlation function Fopen,fread,fscanf,getc
Table header file #include <stdio.h>
Defines the function int getchar (void);
The function Description GetChar () is used to read a character from a standard input device. The character is then converted from unsigned char to int and returned.
The return value GetChar () returns the character read, and if EOF is returned, an error occurs.
Additional Description GetChar () is not a true function, but a getc (STDIN) macro definition.
Example
 code as follows:

#include <stdio.h>
Main ()
{
FILE * FP;
int c,i;
for (i=0li<5;i++)
{
C=getchar ();
Putchar (c);
}
}
Execute 1234/* INPUT * *
1234/* OUTPUT * *

20.gets (read a string from a standard input device)
Correlation function Fopen,fread,fscanf,fgets
Table header file #include <stdio.h>
Defines the function char * gets (char *s);
Function description gets () is used to read characters from a standard device to the memory space referred to by the parameter s until a newline character is present or read to the end of the file, with null ending as a string.
return value gets () returns the S pointer if successful, and returns null to indicate an error occurred.
Additional instructions because gets () cannot know the size of the string s, you must encounter a newline character or end of the file before it ends the entry, which can easily cause a security problem with a buffer overflow. It is recommended to use Fgets () instead.
Example Reference fgets ()

21.mktemp (produces a unique temporary filename)
Correlation function Tmpfile
Table header file #include <stdlib.h>
Defines the function char * mktemp (char * template);
The function Description Mktemp () is used to produce a unique temporary filename. The last six characters in the file name string referred to in parameter template must be xxxxxx. The resulting filename is returned by the string pointer.
When the return value file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code is in errno.
Additional InstructionsThe file name string referred to in parameter template must be declared as an array, such as:
Char template[]= "template-xxxxxx";
Cannot use char * template= "template-xxxxxx";
Example
 code as follows:

#include <stdlib.h>
Main ()
{
Char template[]= "template-xxxxxx";
Mktemp (template);
printf ("template=%s/n", template);
}

22.PUTC (writes a specified character to the file)
Correlation function FOPEN,FWRITE,FSCANF,FPUTC
Table header file #include <stdio.h>
Defines the function int putc (int c,file * stream);
The function Description PUTC () converts parameter C to unsigned char and writes to the file specified by the parameter stream. Although PUTC () is the same as FPUTC (), PUTC () is defined as a macro and is not a real function call.
The return value PUTC () returns the character written successfully, that is, parameter C. If EOF is returned, a write failure is represented.
Example Reference FPUTC ().

23.putchar (write the specified characters to the standard output device)
Correlation function FOPEN,FWRITE,FSCANF,FPUTC
Table header file #include <stdio.h>
Defines the function int putchar (int c);
The function Description Putchar () is used to write the parameter C characters to the standard output device.
The return value, Putchar (), returns the character with the output success, that is, parameter C. If EOF is returned, the output fails.
Additional Description Putchar () is not a true function, but a PUTC (C,STDOUT) macro definition.
Example Reference GetChar ().

24.rewind (reset file stream read-write location begins with file)
Correlation function Fseek,ftell,fgetpos,fsetpos
Table header file #include <stdio.h>
Defines the function void rewind (FILE * stream);
The function Description Rewind () is used to move the read-write location of the file stream to the beginning of the file. The parameter stream is an open file pointer. This function is equivalent to calling Fseek (Stream,0,seek_set).
return value
Example Reference fseek ()

25.setbuf (set buffer area for file stream)
Correlation function Setbuffer,setlinebuf,setvbuf
Table header file #include <stdio.h>
Defines the function void Setbuf (FILE * Stream,char * buf);
Function description The call to Setbuf () can be used to set the buffer for the file stream, before the content is read after the file stream is opened. The parameter stream is the specified file stream, and the parameter buf points to the custom buffer start address. If the parameter buf is a null pointer, there is no buffered IO. Setbuf () is equivalent to calling: Setvbuf (stream,buf,buf?_iofbf:_ionbf,bufsiz)
return value

26.setbuffer (set buffer area for file stream)
Correlation function Setlinebuf,setbuf,setvbuf
Table header file #include <stdio.h>
Defines the function void SetBuffer (FILE * Stream,char * buf,size_t size);
Function description The call to SetBuffer () can be used to set the buffer for the file stream, before the content is read after the file stream is opened. The parameter stream is the specified file stream, and the parameter buf points to the custom buffer start address, and the parameter size is the buffer size.
return value

27.setlinebuf (set file stream to linear buffer)
Correlation function Setbuffer,setbuf,setvbuf
Table header file #include <stdio.h>
Defines the function void Setlinebuf (FILE * stream);
The function Description Setlinebuf () is used to set the buffer io that the file stream is based on in exchange for behavior. Equivalent to call: Setvbuf (Stream, (char *) null,_iolbf,0), refer to Setvbuf ().
return value

28.setvbuf (set buffer area for file stream)
Correlation function Setbuffer,setlinebuf,setbuf
Table header file #include <stdio.h>
Defines the function int setvbuf (FILE * Stream,char * buf,int mode,size_t size);
Function description The call to SETVBUF () can be used to set the buffer for the file stream, before the content is read after the file stream is opened. The parameter stream is the specified file stream, the parameter buf points to the custom buffer start address, the parameter size is the buffer size, and the parameter mode has the following
_IONBF No buffer io
_IOLBF-Free IO based on changing behavior
_IOFBF completely without buffering io. If the parameter buf is a null pointer, there is no buffered IO.
return value

29.ungetc (writes the specified characters back to the file stream)
Correlation function Fputc,getchar,getc
Table header file #include <stdio.h>
Defines the function int ungetc (int c,file * stream);
The function Description Ungetc () writes the parameter C character back to the file stream specified by the parameter stream. This writeback character is obtained by the next function that reads the file stream.
Returns the C character if the return value succeeds, and returns EOF if there is an error.
 code as follows:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *FP = NULL;
char* str;
char re;
int num = 10;
str = (char*) malloc (100);
snprintf (str, ten, "Test:%s", "0123456789012345678");
printf ("str=%s\n", str);
fp = fopen ("/local/test.c", "A +");
if (fp==null) {
printf ("Fail to open file\n");
}
Fseek (Fp,-1,seek_end);
num = Ftell (FP);
printf ("Test file long:%d\n", num);
FSCANF (FP, "%s", str);
printf ("str =%s\n", str);
printf ("Test A:%s\n", str);
while ((RE=GETC (FP))!=eof) {//getc can be used as a fgetc
printf ("%c", re);
}
Fread (STR,10,10,FP);
Fgets (STR,100,FP);
printf ("Test A:%s\n", str);
sprintf (str, "Xiewei test is:%s", "Abcdefghigkmni");
printf ("str2=%s\n", str);
fprintf (FP, "%s\n", str);
Fwrite (STR,2,10,FP);
num = Ftell (FP);
if (str!=null) {
Free (str);
}
Fclose (FP);
return 0;
}

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.