//two ways to write files (text files and binaries)#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>voidReadtxt (Char*path) {//Incoming file Address//Defining file PointersFILE *PF; //read using R in text mode and read using RB in binary modePF = fopen (path,"RB"); //Define a character Charch; //read an elementFread (&ch,1,1, PF); //fread ( buffer array address, array element size 1 bytes, 1 elements per read, file pointer ) while(!feof (PF)) {//continue without reading to the end of the fileprintf"%c", CH); //Continue reading the next elementFread (&ch,1,1, PF); } //Close File pointerfclose (PF);}voidMain () {Charstr[ -] ="I am mad knife to the day to laugh, the liver and gallbladder two Kunlun! The world is drunk and I wake up alone"; //defining text File PointersFILE *PFW; //defining binary file PointersFILE *PFB; //Write a text file Charpathw[ +] ="E:\\look\\w.txt"; Charpathb[ +] ="E:\\look\\b.txt"; //One: Text mode//Open File//w--Write a file, if not, the system will create one, but r--read the file, if not, the file pointer is nullPFW = fopen (PATHW,"W"); if(PFW! =NULL) { //file opened successfully//writes a string to the TXTFwrite (str,1, -, PFW); //fwrite (array first address, array element size 1 bytes, a total of 50 elements, file pointer) } Else{ //File open failedprintf"File open failed \ n"); } //Close File pointerfclose (PFW); //2:2 Binary Mode//Open FilePFB = fopen (PATHB,"WB");//difference, which is using WB if(PFB! =NULL) { //file opened successfully//writes a string to the TXTFwrite (str,1, -, PFB); //fwrite (array first address, array element size 1 bytes, a total of 50 elements, file pointer) } Else{ //File open failedprintf"File open failed \ n"); } //Close File pointerfclose (PFB); printf ("\ n Print out the file \ n"); Readtxt (PATHB); //Presentation FilesSystem"Pause");}
C-language file operation for binary files and text files