C-language file operation for binary files and text files

Source: Internet
Author: User
Tags fread

//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

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.