前言
C++ get()和put()读写文件详解
Code
C++ ostream::put()成员方法
#include<iostream>
#include<fstream>
using namespace std;
int main(){
char c;
ofstream outFile("out.txt",ios::out | ios::binary);
if(!outFile){
cout << "error" << endl;
return 0;
}
while(cin >> c && c!='\n' ){
outFile.put(c);
}
outFile.close();
return 0;
}
这里我没能成功输入文件,命令行操作问题
C++ istream::get()成员方法
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c;
//以二进制形式打开文件
ifstream inFile("out.txt", ios::out | ios::binary);
if (!inFile) {
cout << "error" << endl;
return 0;
}
while ( (c=inFile.get())&&c!=EOF ) //或者 while(inFile.get(c)),对应第二种语法格式
{
cout << c ;
}
inFile.close();
return 0;
}