C language, the operation of the file is important content. The following code implements file creation, read-write, and file display.
On this basis, the file is also displayed in binary mode to reflect how the file is actually stored in the computer.
The specific code implementation process is as follows:
1#include <stdio.h>2#include <stdlib.h>3#include <windows.h>4 intMain ()5 {6FILE *FP;//defines a pointer to a file type variable7 intnum;8 Charname[Ten];//defines a character pointer9 CharPstr//Temp VariableTen Charword[ -];//as a temporary variable to write text One Chartemp[8]; Aprintf"new file name:"); -scanf"%s", name); -Fp=fopen (Name,"w+");//Create a new text document in read-write mode the if(!FP) - { -printf"The file was not created successfully! ");//If there is no file, return the value to the function -Exit1);//Exit Program + } -printf"Please enter text:");//prompt to enter text +scanf"%s", word);//whitespace also as a string terminator A //gets (word);//only carriage return as a string terminator atFputs (WORD,FP);//writing a string to a file -printf"the contents of the file (shown in text form) are:"); - //Rewind (FP);//The file pointer points to the beginning -Fseek (FP,0, Seek_set); - while((PSTR=FGETC (fp))! =EOF) - { inPutchar (PSTR);//Show file Contents - } to Rewind (FP); +printf"\ n is displayed in binary form as:"); - while((PSTR=FGETC (fp))! =EOF) the { *Num= (int) Pstr;//convert character to ASCII code $Itoa (Num,temp,2);Panax Notoginsengprintf"%s", temp); - } theFclose (FP);//Close File + return 0; A}
C language-Read and write files and output