C language-Read and write files and output

Source: Internet
Author: User
Tags rewind

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

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.