c language file operations (rpm)

(Article Categories "c / c ++" there, but only the basic instructions, afraid of the future forgotten. See this relatively elaborate on the left to be a memo)

C language file manipulation functions

Category: C / C
13.1C language document
1, two file access mode (input, output)
sequential access
direct access
2, two kinds of data stored in the form of
a text file
binaries
 
 
The file pointer 13.2
general form definition file type of pointer variables:
the FILE * pointer variable name;
for example:
the FILE * fp1,* fp2;
 
 
13.3 open a file
before using files, open the file using the fopen function opens the file in C format:..
Fopen (filename, file use);
This function returns a FILE type pointer to such:.
FILE * FP ;
fp = fopen ( "file_1", "r");
If the call succeeds, fp points to file_1, otherwise it returns NULL, so in order to ensure the proper use of documents, to be tested using the following statement:.
the If (( fp = fopen ( "file_1", "R & lt")) == NULL)
{
the printf ( "Can the this file Not Open \ n-");
Exit (0);
}
the most common use of the file and their meanings are as follows:
. 1, "R & lt" is. read a text file is opened. (does not exist error)
2, "rb." binary file is opened for reading.
3, "w." opened for writing text files. (if there is new construction, on the contrary, from the file write starting position, the original content is overwritten)
. 4, "WB." opened for writing binary file.
. 5, "A." To add data to the file and the text file is opened (if there is new; the other hand, in the original file is appended).
. 6, "ab" is added after the data file and opens a binary file.
The most common use of the file and have the following meanings:
... 7, "R & lt +" to open a text file read and write (read, the scratch ; to write data, the new data covers a space occupied by the same thereafter)
. 8, "RB +" for the reading and writing binary file is opened only during subsequent read and write, may be read and set by a function of position. write starting position.
9, "w +". first create a new file, write, and then you can start from scratch read. (If the file exists, the original content will disappear)
10, "wb +." function and "w +" with only subsequent read and write, read and write starting position can be provided by a function of position.
use the most common files and their meanings are as follows:
.. 11, "a +" and the feature "a" the same; only the file after adding a new tail data may be read from the beginning.
12 is, "ab & +" function and "a +" the same;. only after the tail of the new data file is added, the read start position may be set by a function of position.
 
 
13.4 close the file
When the write operation is completed file, using fclose function closes the file has:.
Fclose (file pointer)
as: fclose ( FP );
 

13.5 call getc (fgetc) and putc (fputc) function input and output
a call putc (or of fputc) function to output a character
call form:
putc (ch, fp );
function: ch written character file pointer fp file referred to when the output of the success, the function returns putc character output; otherwise, return an EOF is a symbolic constant value .EOF defined in stdio.h file library function, whose value is equal to -1.
 
 
13.5 calls getc (fgetc) and putc (fputc) for input and output functions
such as: the text entered from the keyboard as it is output to a file named file_1.dat, ending with the character @ sign as keyboard input.
#Include
Void main ( )
{
the FILE * fpout;
char CH;
IF (fpout = fpopen ( "file_1", "W") == NULL)
{
"! Can Not Open the this File \ n-" the printf ();
Exit (0);
}
CH = getchar ();
the while (! CH = '@')
{of fputc (CH, fpout); CH = getchar ();}
fclose (fpout);
}
2. getc call (or fgetc) a character input function
call of the form:
CH = getc (pf);
function is: pf specified file is read from such a character, and it functions as a return value.
For example: the content is a text file on disk file_1.dat the present, is output to the terminal on the screen.
#include
void main () {
FILE *fpin;
char ch;
if((fpin=fopen("file_1.dat","r"))==NULL)
{ printf("Cann't open this file!\n");exit(0);}
ch=fgetc(fpin);
while (ch!=EOF)
{ putchar(ch); ch=fgetc(fpin);}
fclose(fpin);
}
 

13.6 End Analyzing file function feof
the EOF flag as the end of a text file, but can not function as a terminator .feof Analyzing binaries may be a binary file, a text file and can be determined.
Example: programming for converting a text file ( source) to another file (object), a source file name and file name from the object of the command line command in the form as follows:
executable program name, the file name of the source file names
#include
void FileCopy (the fILE *, the fILE *);
void main (int argc, char * the argv []) {
the FILE * fpin, * fpout;
IF (argc ==. 3)
{= fpin the fopen (the argv [. 1], "R & lt");
fpout = the fopen (the argv [2], "W");
FileCopy (fpin, fpout);
fclose (fpin); fclose (fpout);
}
the else IF (argc>. 3)
the printf ( "File names of The TOO MANY !! \ n-";
the else
the printf ( "There are File names for INPUT or NO Output !! \ n-);
}
void FileCopy (fpin the FILE *, FILE *fpout)
{
char ch;
ch=getc(fpin);
while(!feof(fpin))
{putc(ch,fpout); ch=getc(fpin);}
}
 

13.7fscanf functions and fprintf function
1, fscanf function
fscanf only from a text file as input format, and scanf function similar to the object but the input data is a text file on the disk in the form of call:.
Fscanf (file pointer, format control character string, the entry table)
, for example: the fscanf ( FP , "% D% D", & A, & B);
the fscanf (stdin, "% D% D", & A, & B);
equivalent to scanf ( "% d D% ", & a, & B);
3.fprintf function
fprintf function to convert the format of data in memory to the corresponding character, and outputs to a text file and .Fprintf similarity function printf function in ASCII code form, except that the output the content stored in the format of a text file called disk form as follows:
fprintf (file pointer, format control character string, the output item table)
, such as: fprintf ( FP , "% D% D", X, Y);
the following statement fprintf (stdout, "% d% d ", x, y)
 

13.8fgets function and the function fputs
1, fgets function
fgets function for reading from a file into a string form the following call:.
Fgets (STR, n-, fp );
function function is: from fp to read the file referred to n-1 str is a character into a space start address; unread if the full n-1 characters, a newline or EOF end of the read operation, and has a function as a return value str.
13.8fgets function fputs function and
2, fputs function
. fput function string output to a file function call the following form:
fputs (STR, FP );
Note: for ease of reading, in the output string, it should be added, such as artificial "\ n "this kind of string.
#include <stdio.h>
#include <iostream>
int main(int argc, char *argv[])
{
 char arr[10] ;
 char *ap = "hello!" ;
 FILE *fp ;
 if ((fp = fopen("hello.txt", "wt+")) == NULL)
 {
  printf("error!") ;
  exit(1) ;
 }
    fputs(ap, fp) ;

 rewind(fp) ; //

 fgets(arr, 10, fp) ;
 printf("%s\n", arr) ;
 fclose(fp) ;
 return 0 ;
}


13.9fread function and the function fwrite
example has the following structure:
struct {ST
char NUM [. 8];
a float MK [. 5];
} Pers [30];
hereinafter this data is output to the loop elements 30 fp document referred . in
for (I = 0; I <30; I ++)
fwrite (& Pers [I], the sizeof (struct ST),. 1, FP );
 

13.9fread function and the function fwrite
following statement from fp to each student clocked into the data file referred to in the array again pers.
I = 0;
fread (& pers [I], the sizeof (struct ST),. 1, fp ) ;
the while (feof (! FP ))
{I ++;
fread (& Pers [I], the sizeof (struct ST),. 1, FP );
}
 

13.10 file positioning function
1, fseek function
fseek function is used to move the pointer to the file location specified location, and then read or write operations from here on in function calls is as follows:.
fseek (PF, offset, Origin)
PF: file pointer
offset: in bytes the amount of displacement units, as long integer.
Origin:. is the starting point, which is used to specify the amount of displacement of the position as a reference
. 1, fseek function of
the amount of displacement of the representation of
the identifier number representing the starting point
SEEK_SET 0 start file
SEEK_END 2 end of file
SEEK_CUR 1 current position of the file
is assumed to point pf has a binary file, then;
fseek (pf,. 3 0L , SEEK_SET)
fseek (PF, -1 0L * the sizeof (int), SEEK_END )
For text files, the amount of displacement must be 0; if:
fseek (PF, 0L , SEEK_SET)
fseek (PF, 0L , SEEK_END )
2. ftell function of
the position of the file ftell function to obtain the current position of the pointer, the function given the current number of bytes relative to the beginning of the file pointer, such as the location;.
Long T;
T = ftell (PF);
when an error function calls, function returns -1L .
, we can test the size of a file in the following manner:
fseek ( FP , 0L , SEEK_END );
T = ftell ( FP );
3.rewind function
Call in the form of:
rewind (PF);
function does not return value of the function is a function of the location of the file pointer back to the beginning of the file.
 

13.10 Application files
on disk file test.txt 10 placed a positive integer not less than 2, the programming required to achieve a function call:
1, the prime of the called function, and determines integer of statistics 10 and the number of prime numbers.
2, in the main function is added to the end of all primes test.txt disk file, while the output to the screen.
#include
#include
int prime (a int [], int n-)
{
int the I, J , K = 0, In Flag = 0;
for (I = 0; I {for (J = 2; J IF (A [I]% J == 0)
{In Flag = 0; BREAK;}
the else In Flag =. 1;
IF (In Flag)
{A [K] = A [I]; K ++;}
}
return K;
}
void main () {
int n-, the I, A [10];
the FILE * FP ;
FP = the fopen ( "test1-2. TXT "," R & lt + ");
for (n-= 0; n-<10; n-++)
the fscanf ( FP,"%d",&a[n]);
n=prime(a,n);
fseek( fp,o,2);
for(i=0;i {printf("%3d",a[i]);
fprintf( fp,"%3d",a[i]);
}
fclose( fp);
}

Reproduced in: https: //www.cnblogs.com/nniixl/archive/2007/04/23/724235.html

Guess you like

Origin blog.csdn.net/weixin_34270606/article/details/94467946