两个方法
fin.read(内存地址 char*,字节数);
注意这里是char* 需要做类型转换,
reinterpret_cast<char*>(…..);
fout.write(内存地址 char*,字节数);
在C语言中,fread(内存地址,单位大小,总个数,文件)和fwrite是将数据在文件与内存中间移动处理,对应C++的为fin.read(内存地址 注意这里需要强制类型转换,字节数) 和 fout.write(内存地址,字节数)。
fin.gcount()最后一次read操作读到几个字节
#include <iostream>
#include <string>
#include <fstream>//文件流
using namespace std;
int main(){
string path = "kak.txt";
ifstream fin(path.c_str());
/*
等价于
ifstream fin;
fin.open(path.c_str());
*/
if(!fin){
cout << "打开失败" << endl;
return 1;
}
char c;
fin.get(c);//原样读取
fin >> c; //跳过空白字符读取
fin.close();
getchar();
return 0;
}
用gcount来统计读出的字节数,以防止写入时出错
fin.read(buf, sizeof(buf));
fout.write(buf, fin.fcount());