Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Where is st plastic surgery
Where is st plastic surgery
I'd better practice this by myself. I'll teach you in detail with MSDN. There are concrete examples.

1C language file

1 and 1 file access mode (input and output mode)

sequential access

direct access

textfile

binary file

2 file pointer

Defines the general form of a file type pointer variable:

FILE * pointer variable name;

For example:

FILE *fp 1,* fp2

3 Open the file

You need to open the file before using it. Use fopen function in C to open the file. The format is:

Fopen (file name, file purpose);

This function returns a pointer to the file type. For example:

FILE * fp

fp=fopen("file_ 1 "," r ");

If the call is successful, fp will point to file_ 1, otherwise it will return NULL, so in order to ensure the correct use of the file, it should be tested. Adopted the following statement:

If((fp=fopen("file_ 1 "," r ")= = NULL)

{

Printf ("Cannot open this file/n");

Exit (0);

}

The most commonly used file usage methods and their meanings are as follows:

1," r "。 Open the text file for reading. (Error if it does not exist)

2、“rb”。 Open the binary file for reading.

3、“w”。 Open a text file for writing. (If it doesn't exist, create it; otherwise, start from the file and overwrite the original content. )

4、“wb”。 Open the binary file for writing.

5、“a”。 Open a text file and add data at the back of the file. (If it doesn't exist, create a new one; Instead, it is added after the original document)

6、“ab”。 Open a binary file and add data at the back of the file.

The most commonly used file usage methods and their meanings are as follows:

7、“r+”。 Open a text file for reading and writing. (When reading, start from the beginning; When writing data, the new data only covers the occupied space, and then remains unchanged)

8、“rb+”。 Open the binary file for reading and writing. Only in the subsequent reading and writing, the starting position of reading and writing can be set by the position function.

9、“w+”。 First, create a new file, write it, and then read it from scratch. (If the file exists, all the original contents will disappear. )

10,“wb+”。 The function is the same as "w+". Only in the subsequent reading and writing, the starting position of reading and writing can be set by the position function.

The most commonly used file usage methods and their meanings are as follows:

1 1,“a+”。 Function is the same as "a"; Only after adding new data at the end of the file can you read from the beginning.

12,“ab+”。 The function is the same as "a+"; Only after adding new data at the end of the file can the starting position of reading be set by the position function.

4 Close the file

When the reading and writing operation of the file is completed, use the fclose function to close the file. The format is as follows:

file pointer

Such as: fclose (FP);

5 Call getc(fgetc) and putc(fputc) functions for input and output.

1, call the putc (or fputc) function to output a character.

Call form is:

putc(ch,FP);

The function is to write the character ch into the file pointed by the file pointer fp. When the output is successful, the putc function returns the output character; Otherwise, an EOF value is returned. EOF is a symbolic constant defined in the stdio.h library function file, and its value is equal to-1.

6. Call getc(fgetc) and putc(fputc) functions for input and output.

For example, the text input from the keyboard is output as it is to a file named file_ 1.dat, and the character @ is used as the ending symbol of keyboard input.

# Including

Void main()

{

FILE * fpout

char ch

if(fpout=fpopen("file_ 1 "," w")==NULL)

{

Printf ("Cannot open this file! /n ");

Exit (0);

}

ch = getchar();

And (ch! ='@')

{ fputc(ch,FP out); ch = getchar(); }

fclose(FP out);

}

2. Call the getc (or fgetc) function to enter a character.

Call form is:

ch = getc(pf);

Function: Read a character from the file specified by pf and return it as a function value.

For example, output the contents of the file_ 1.dat text file existing on the disk to the terminal screen as it is.

# Including

void main(){

FILE * fpin

char ch

if((fpin = fopen(" file _ 1 . dat "," r ")= = NULL)

{printf ("Can't open this file! /n "); Exit (0); }

ch = fgetc(fpin);

And (ch! =EOF)

{ putchar(ch); ch = fgetc(fpin); }

fclose(fpin);

}

6 judge the file end function feof

EOF can be used as the end symbol of text files, but not as the end symbol of binary files. The feof function can judge both binary files and text files.

Example: Write a program to copy a text file (source) to another file (target). The source file name and target file name are entered by the command line. The command form is as follows:

Executable program name source file name file name

# Including

void filecopy(FILE*,FILE *);

void main(int argc,char *argv[]){

FILE *fpin,* fpout

if(argc==3)

{ fpin=fopen(argv[ 1]," r ");

fpout=fopen(argv[2]," w ");

filecopy(fpin,FP out);

fclose(fpin); fclose(FP out);

}

else if(argc & gt; 3)

Printf ("Too many file names! ! /n ";

other

Printf ("No input or output file name! ! /n);

}

void filecopy(FILE *fpin,FILE *fpout)

{

char ch

ch = getc(fpin);

And (! feof(fpin))

{putc(ch,FP out); ch = getc(fpin); }

}

7fscanf function and fprintf function

1, fscanf function

Fscanf can only be input from a formatted text file, which is similar to the scanf function, except that the input object is the data in the text file on disk. The telephone form is:

Fscanf (file pointer, format control string, entry list)

For example: fscanf (fp, "%d% d",&; I. & AMPB);

Fscanf (standard input, "%d%d", & i, & amp);

Equivalent to scanf ("%d% d ",&; I. & AMPB);

3.fprintf function

The Fprintf function converts the data in memory into corresponding characters according to the format, and outputs them to a text file in the form of ASCII code. The fprintf function is similar to the printf function, except that the output is stored in a text file on disk according to the format. Call form is as follows:

Fprintf (file pointer, format control string, output item table)

Such as fprintf(fp, "%d %d", x, y);

The following statement fprintf(stdout, "%d %d", x, y)

8fgets function and fputs function.

1, fgets function

The function reads a string from a file. The call form is as follows:

fgets(str,n,FP);

The function is to read n- 1 characters from the file pointed by fp and put them in the space with str as the initial address; If n- 1 characters are not completely read, a line break or EOF is encountered to end the reading operation, and str is returned as a function value.

13.8fgets function and fputs function

2. fputs function

The fput function outputs a string to a file. The function call form is as follows:

fputs(str,FP);

Note: In order to facilitate reading, when outputting strings, you should artificially add strings such as "/n".

9 read function and write function

Fread and fwrite functions are used to read and write binary files. They are called as follows:

Fread (buffer, size, count, FP);

Fwrite (buffer, size, count, FP);

Buffer: the first address of the input or output data block.

Count: Number of input or output data blocks per read and write.

Size: Number of bytes per data block.

file pointer

If you have the following structure:

Structure st{

Character number [8];

float MK[5];

} pers[30];

The following loop will output the data in these 30 elements to the file indicated by fp.

for(I = 0; I & lt30; i++)

fwrite(& amp; pers[i],sizeof(struct st), 1,FP);

9 read function and write function

The following statement reads each student data into pers array one by one from the file pointed by fp.

I = 0;

Fred (& amps [i], sizeof(struct st), 1, fp);

And (! feof(fp))

{ i++;

Fred (& amps [i], sizeof(struct st), 1, fp);

}

10 file location function

1, fseek function

The fseek function is used to move the file location pointer to the specified location, where subsequent read or write operations will start. This function is called as follows:

Fseek(pf, offset, origin)

Pf: file pointer

Offset: the displacement in bytes, which is a long integer.

Origin: is the starting point, which is used to specify where the displacement is based.

1, fseek function

Representation method of displacement

The starting point indicated by the identifier number

Start of SEEK_SET 0 file

End of SEEK_END 2 file

SEEK_CUR 1 current location of the file

Assuming that pf has pointed to a binary file, then;

fseek(pf,30L,SEEK_SET)

fseek(pf,- 10L*sizeof(int),SEEK_END)

For text files, the offset must be 0; For example:

fseek(pf,0L,SEEK_SET)

fseek(pf,0L,SEEK_END)

2.ftell function

The ftell function is used to get the position of the current position pointer of the file, which gives the number of bytes of the current position pointer relative to the beginning of the file.

Long t;

t = ftell(pf);

When the function call fails, the function returns-1l.

We can test the length of the file in the following ways:

fseek(fp,0L,SEEK _ END);

t = ftell(FP);

3. Rewind function

Call form is:

Rewind (pf);

This function does not return a value. The function returns the position pointer of the file to the beginning of the file.

10 file application

There are 10 positive integers not less than 2 in the test.txt file on the disk, and the program is written by function call. Need to realize:

1, in the adjusted function prime, judge and count the prime numbers and numbers in 10 integers.

2. Append all prime numbers to the end of the disk file test.txt in the main function and output them to the screen at the same time.

# Including

# Including

Int prime(int a[],int n)

{

int I,j,k=0,flag = 0;

for(I = 0; I { for(j = 2; j if(a[i]%j==0)

{ flag = 0; Break; }

else flag = 1;

If (logo)

{ a[k]= a[I]; k++; }

}

Return k;

}

void main(){

int n,I,a[ 10];

FILE * fp

fp=fopen("test 1-2.txt "," r+");

for(n = 0; n & lt 10; n++)

fscanf(fp," %d ",& ampa[n]);

N = prime number (a, n);

fseek(fp,o,2);

for(I = 0; i {printf("%3d ",a[I]);

fprintf(fp," %3d ",a[I]);

}

fclose(FP);

}