C + + read and write to TXT file

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/lh3325251325/article/details/4761575

1#include <iostream>2#include <iomanip>3#include <fstream>4  5 using namespacestd; 6   7 intMain () {8 Charbuffer[ the]; 9Ifstream MyFile ("C://a.txt"); TenOfstream outfile ("C://b.txt");  One    A if(!myfile) {   -cout <<"Unable to open myfile";  -Exit1);//Terminate with Error the    - }   - if(!outfile) {   -cout <<"Unable to open Otfile";  +Exit1);//Terminate with Error -   + }   A intb;  at intI=0, j=0;  - intdata[6][2];  -    while(!myfile.eof ()) -   {   -Myfile.getline (Buffer,Ten);  -SSCANF (Buffer,"%d%d",&a,&b);  incout<<a<<" "<<b<<Endl;  -data[i][0]=A;  todata[i][1]=b;  +i++;  -   }   the Myfile.close ();  *    for(intk=0; k<i;k++){   $outfile<<data[k][0] <<" "<<data[k][1]<<Endl; Panax Notoginsengcout<<data[k][0] <<" "<<data[k][1]<<Endl;  -   }   the    + Outfile.close ();  A return 0;  the }   +    -   $   $    -This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/dragonworrior/archive/2009/11/02/4759484.aspx

Include <fstream> header files, both read and write

READ: Read data from an external file into the program for processing

For a program, the data is read from the outside, so define the input stream, which defines the input stream object: Ifsteam infile,infile is the input stream object.

This object holds the data stream that is about to be read from the file. If there is a file named MyFile.txt, there are two lines of digital data, the specific method:

1 intb; 2 Ifstream infile; 3Infile.open ("MyFile.txt");//note the path to the file4infile>>a>>b;//two rows of data can be read continuously in the variable5 infile.close ()6   7 //if it is a large multi-line stored text file, you can read this:8 Charbuf[1024x768];//temporarily save the contents of a read file9 stringmessage; Ten Ifstream infile;  OneInfile.open ("Myfile.js");  A if(Infile.is_open ())//the file opened successfully, stating that it had been written . - {   -  while(Infile.good () &&!infile.eof ()) the {   -memset (BUF,0,1024x768);  -Infile.getline (BUF,1204);  -Message =buf;  +......//There may be some manipulation of the message -cout<<message<<Endl;  + }   A Infile.close ();  at}

Write: Writes the processed data in the program to the file
To the program is to write the data out, that is, the data out of the program, so the definition of the output stream object Ofstream Outfile,outfile is the output stream object, which is used to store the data will be written to the file. Specific practices:

1 ofstream outfile; 2Outfile.open ("Myfile.bat");//Myfile.bat is the file name that holds the data3 if(Outfile.is_open ())4 {  5outfile<<message<<endl;//message is the data processed in the program6 Outfile.close (); 7 }  8 Else  9 {  Tencout<<"cannot open file!"<<Endl;  One}

Examples of C + + read and write operations to files

1 /*/Read a line of characters from the keyboard, place the letters in the disk file Fa2.dat, and then read it from the disk file into the program,2 change the lowercase letter to uppercase and then into disk Fa3.dat*/   3#include <fstream>4#include <iostream>5#include <cmath>6 using namespacestd; 7 //////////////function to read characters from the keyboard8 voidRead_save () {9       Charc[ the]; TenOfstream outfile ("F1.dat");//Open the file with the output worker One       if(!outfile) {   Acerr<<"Open error!"<<endl;//Note that it's cerr. -Exit1);  -                    }   theCin.getline (c, the);//reads a line of characters from the keyboard -            for(intI=0; c[i]!=0; i++)//one-to-one processing of characters until '/0 ' is encountered -                 if(c[i]>= $&&c[i]<= -|| c[i]>= the&&c[i]<=122){//guarantees that the characters entered are characters -Outfile.put (C[i]);//save alphabetic characters to disk file +cout<<c[i]<<"";  -                   }   +cout<<Endl;  A Outfile.close ();  at                    }   - voidCreat_data () { -       Charch;  -Ifstream infile ("F1.dat"Ios::inch);//Open the file as input -       if(!infile) {   -cerr<<"Open error!"<<Endl;  inExit1);  -                   }   toOfstream outfile ("F3.dat");//defining the output stream F3.dat file +     if(!outfile) {   -cerr<<"Open error!"<<Endl;  theExit1);  *                  }   $       while(infile.Get(CH)) {//When you read the word Fu ChenggongPanax Notoginseng      if(ch<=122&&ch>= the)   -ch=ch- +;  the outfile.put (CH);  +cout<<ch;  A      }   thecout<<Endl;  + Infile.close ();  - Outfile.close ();  $      }   $      intMain () { - Read_save ();  - Creat_data ();  theSystem"Pause");  -          return 0; Wuyi}

C + + read and write to TXT file

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.