C + + read and write files

Source: Internet
Author: User

#include <iostream>#include<fstream>#include<string>intMain () {using namespacestd; stringfilename; cout<<"Enter Name of the new file:"<<Endl; CIN>>filename; //Type 1//ofstream Fout (Filename.c_str ()); //Type 2Ofstream Fout; Fout.open (Filename.c_str ()); //If a file name is declared as "string", then you must use "C_str", however, when you declare that a file name is a character array type, there is no need to use/Fout<<"This is my test of file read and write \ n" ;    Fout.close (); //**********read*****************//ifstream Fin (FILENAME.C_STR ()); Charch; //Type 1//While (Fin.get (CH))//read one character at a time to CH//cout << Ch <<endl; //Type 2Fin >> ch;//reads a character T    stringstr; Fin>> str;//Read thiscout << str <<Endl; //Type 3//Char ch2[100]; //Fin.getline (ch2,100); //int i = 0; //While (i<40)//{    //cout << "ch2[" << I << "]:" <<ch2[i]<< Endl; //i++; //}        //Type 4//string buff; //getline (Fin,buff); //cout<<buff<< "\ n"; //cout << "done\n";    return 0;}

C + + read and write files

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.