C, c + + file operations-file concepts and file writing

Source: Internet
Author: User
Tags sprintf

Files are used to persist data

Persistent: Data does not disappear after power failure. Next time you add, you can read

Properties of the file:

Filename

Path

Length (measured in bytes)

Content

Read-only/read-write

fopen Open a file

Fclose closing files

Fwrite writing data

Fread closing files

Steps to save data to a file

1) fopen Open File

FILE *fopen (const char * filename, const char * mode);

Mode uses WB, writes binary

2) fwrite Write data

size_t fwrite (const void * buf, size_t size, size_t count, FILE * stream);

BUF the data to write

Size is always 1

Count length

Stream fopen the created pointer

3) fclose Close file

Simple example of writing data

#include <stdio.h>intMain () {//Define file name    Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully    if(fp = =NULL) {printf ("failed to open file!\n"); return-1; }          CharBuf[] ="Hello"; //Write DataFwrite (BUF,1,5, FP); intA =0x12345678; Fwrite (&a,1,4, FP);        Fclose (FP); return 0;} 

Write Data in detail

I. Writing data to an array

The code is as follows

#include <stdio.h>#include<string.h>intMain () {//Define file name    Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully    if(fp = =NULL) {printf ("failed to open file!\n"); return-1; }          intbuf[4] = {0xa001,0xb002,0xc003,0xd004};
for(inti =0; I <4; i++) { Chartext[ -]; //convert int type data to character typesprintf (Text,"%d,", Buf[i]); Fwrite (text,1, strlen (text), FP); } fclose (FP); return 0;}

Two. Specify format to save decimals

#include <stdio.h>#include<string.h>intMain () {//Define file name    Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully    if(fp = =NULL) {printf ("failed to open file!\n"); return-1; }         DoubleA =3/4.0; Chartext[ -]; //keep two decimal placessprintf (Text,"%.2LF", a); Fwrite (text,1, strlen (text), FP);        Fclose (FP); return 0;}

Three. Writing a string

Two ways to store strings

char buf [+] = "Hello";

Method 1: Store by actual effective length (using the Strlen function inside the string.h to get the string specific length)

Fwrite (buf, 1, strlen (BUF), FP);

Mode 2: Write by fixed length

Fwrite (BUF, 1, (+), FP);

(More space occupied)

#include <stdio.h>#include<string.h>intMain () {//Define file name    Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully    if(fp = =NULL) {printf ("failed to open file!\n"); return-1; }    Charbuf[ -] ="Hello"; //fwrite (Buf,1,strlen (BUF), FP);
  Fwrite (buf,1,
return 0 ;

}

Four. Writing to a struct

#include <stdio.h>#include<string.h>structstudent{intID; Chargender; Charname[ -];};intMain () {//Define file name    Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully    if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } Student someone= {61031,'M',"Noname"}; //fwrite (&someone, 1, sizeof (someone), FP);fwrite (&someone.gender,1,1, FP); Fwrite (&someone.id,1,4, FP); Fwrite (&someone.gender,1, -, FP);    Fclose (FP); return 0;}

fopen (filename, "WB");

When the parameter is WB, the content will overwrite

When the parameter is AB, the written content is appended to the back

C, c + + file operations-file concepts and file writing

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.