C Language Review 3/27

Source: Internet
Author: User
Tags file type file fread

1. Pointers pointing to pointers

#include <stdio.h>intMain () {Char*a[] = {"Hello"," World","my"," Love" }; Char**p;//*p indicates that p is a pointer variable, and char * represents a variable with p pointing to char * typep = A;//p = &a[0]printf"%s\n", *p);//output A[0]    return 0;}

2. Function pointers

Int (*p) (int, int);//define a function pointer p, the function that points to has two parameters of type int, the function return value is type int.

#include <stdio.h>intMain () {void(*p) (int,int); voidMaxint,int); voidMinint,int); intn =0; scanf_s ("%d", &N); if(n = =1) P=Max; ElseP=min; (*P) (3,4); return 0;}voidMaxintXinty) {printf ("%d\n", x > y?x:y);}voidMinintXinty) {printf ("%d\n", x < y?x:y);}

A function pointer is the entry address of a function.

3. Structural body

/*Method One*/structstudent{...} Student1,student2;/*Method Two*/structstudent{...};structStudent Student1, Student2;//struct student is a type name/*Method Three*/typedefstruct{    ...} Student; Student student1, Student2;/*method Four*/struct{      ...} Student1,student2;

4. File Type pointers

typedefstruct{     ShortLevel//The extent to which the buffer is "full" or "empty"unsigned flags;//file Status Flag    CharFd//File DescriptorUnsignedCharHold//no content, such as buffer, does not read characters     ShortBsize;//size of the bufferUnsignedChar* Buffer;//the location of the data bufferUnsignedChar* CURP;//Pointer to the current pointunsigned istemp;//Temp File Indicator     ShortToken//for validity checks}file;

Information that declares the file struct type is contained in the header file "Stdio.h". A variable can be defined directly in a program using the file type name, and each file type variable corresponds to the information area of a document in which information about the file is stored.

File *fp;//defines a pointer variable fp that points to files;

4. File operation

    • fopen (filename, using file mode);

Typically, the return value of the fopen function is assigned to a pointer variable that points to the file.

FILE *FP;

fp = fopen ("A1", "R");

In this way, the FP is associated with the file A1, or FP points to the A1 file

When the computer reads a character from an ASCII file, it encounters a carriage return newline character, which is converted to a newline character, and the line break is converted to carriage return and newline two characters at output. When using binary files, this conversion is not performed, and the data form in memory is exactly the same as the data in the output to the external file.

fclose (file pointer);//If the function executes successfully returns 0, it returns EOF (i.e.-1)

    • Fclose (FP);

If you do not close the file, you will lose data. Because, when writing data to a file, the data is output to a buffer before it is formally exported to the file until the buffer is full. If the data is not filled with buffers and the program finishes running, it is possible that the data in the buffer is lost. Using the Fclose function to close the file, the data in the buffer is output to a disk file before the file information area is revoked. Some compiled systems avoid this problem by automatically writing the data in the buffer to the file before the program ends, but you should develop the habit of closing all files before the program terminates.

    • Read characters to a file

FGETC (FP);//read a character from the FP-pointing file, read it successfully, bring back the read character, or return EOF

FPUTC (CH,FP);//write the character ch to the file that the file pointer variable FP refers to, the output is successful, the return value is the output character, the output fails, then the EOF is returned

#include <stdio.h>#include<stdlib.h>intMain () {FILE*FP; CharCH, filename[ -]; printf ("Please enter the name of the file you are using:"); scanf ("%s", filename); if(fp = fopen (filename,"W")) ==NULL) {printf ("cannot open this file \ n"); Exit (0); } CH= GetChar ();//to receive a carriage return after the file name has been enteredprintf"Please enter a string to be saved to disk (end with #):"); CH=GetChar ();  while(ch! ='#') {FPUTC (ch, FP);        Putchar (CH); CH=GetChar ();    } fclose (FP); Putchar (Ten);//output a line break to the screen    return 0;}
Copy the contents of one file into another file
#include <stdio.h>#include<stdlib.h>intMain () {FILE*fp_in,*fp_out; CharCH, filename_in[ -],filename_out[ -]; printf ("Please enter a file name to read in:"); scanf ("%s", filename_in); if(fp_in = fopen (filename_in,"R")) ==NULL) {printf ("cannot open this file \ n"); Exit (0); } printf ("Please enter the file name written out:"); scanf ("%s", filename_out); if(Fp_out = fopen (Filename_out,"W")) ==NULL) {printf ("cannot open this file \ n"); Exit (0); } while(!feof (fp_in))//feof function is to check whether to the end of the file {ch=fgetc (fp_in); FPUTC (CH, fp_out); Putchar (CH); } Putchar (Ten); Fclose (fp_in); Fclose (fp_out); return 0;}

Note: The C system has defined the FPUTC and FGETC functions as macro names PUTC and getc

#define PUTC (CH,FP) FPUTC (CH,FP)

This is defined in the stdio.h.

    • Read and write a string to a file

char* fgets (char* str,int n,file* FP);

Read the n-1 characters from the file that the FP points to, add ' \ s ' to the string, and put them in the character array str. If you encounter a newline character ' \ n ' or a file terminator eof before you finish reading n-1 characters, the read-in is finished, but the newline character encountered is ' \ n ' also read as a character. If the Fgets function succeeds, the return value is the address of the first element of the STR array, and NULL is returned if there is an error at the beginning of the end of the file or read data.

int puts (char* str, file* FP);

Outputs the string pointed to by STR to the file that the FP points to. The string at the end of the ' + ' does not output. If the output succeeds, the function value is 0, and if it fails, the function value is EOF.

    • Read and write a set of data to a file in a binary way

The C language allows you to read a block of data from a file using the Fread function and write a block of data to the file using the Fwrite function. It is performed in binary form when reading and writing. When writing data to a disk, a set of data in memory is copied directly to the disk file intact, unaltered, and read into memory as a batch of several bytes in the disk file. The general invocation form is:

Fread (BUFFER,SIZE,COUNT,FP);

Fwrite (BUFFER,SIZE,COUNT,FP);

which

Buffer: is an address. For Fread, it is the store address for storing data read from a file. For fwrite, the data in the store where this address starts is to be output to the file (above refers to the start address).

Size: Number of bytes to read and write

Count: Number of data items to read and write (each data item length is size)

Fp:file type file pointer

A binary file is specified when the punch-in file is used, so that any type of information can be read and written using the Fread and fwrite functions.

C Language Review 3/27

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.