标签:while 数组 std close write his 读写 clu names
#include <iostream> #include <fstream> #include <string> int main() { using namespace std; string filename; cout << "Enter name of new file:" << endl; cin >> filename; //type 1 //ofstream fout(filename.c_str()); //type 2 ofstream fout; fout.open(filename.c_str()); //如果一个文件名被申明为“string”,那么就必须使用 “c_str”,然而,当你申明一个文件名为字符数组型,就没有必要使用/ fout << "this is my test of file read and write \n" ; fout.close(); //**********read*****************// ifstream fin(filename.c_str()); char ch; // type 1 //while (fin.get(ch)) //每次读取一个字符到ch // cout << ch <<endl; //type 2 fin >> ch; // 读取一个字符 t string str; fin >> str; //读取 this cout << 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\n"; //cout << "Done\n"; return 0; }
标签:while 数组 std close write his 读写 clu names
原文地址:http://www.cnblogs.com/shaogang/p/6785897.html