#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ofstream outFile("d:\\test.txt",ofstream::out|ofstream::app);
if(!outFile){cout<<"Open file failed !"<<endl; }else{cout<<"Open file suc!"<<endl;
}
//写入固定的str
outFile<<"First!"<<endl;
//键入文件
char ch;
while(cin.get(ch)){
if(ch == '\n')break;
outFile.put(ch);
}
outFile.close();
ifstream readFile("d:\\test.txt",ios::in);
if(!readFile){cout<<"read error!";return 0;
} else cout<<"read suc"<<endl;
//读取文本中所有的内容
char c;
readFile.get(c);
cout<<"get-------------->"<<c<<endl;
char str[100];
while(readFile.getline(str,100)) {
cout<<str<<endl;
}
readFile.close();
//binary二进制形式传送和存储。用read函数和write函数
// read(char *buf,int len); write(const char *buf,int len);
fstream binaryFile("d:\\test.txt",ios::out|ios::binary|ios::in);
if(!binaryFile){cout<<"read error!";return 0;
} else cout<<"read suc"<<endl;
char arr[13] = "hello world!";
binaryFile.write(arr,13);
binaryFile.seekg(ios::beg);// 定位至文件首部
char read_array[13] ;
binaryFile.read(read_array,13);
cout<<"----------"<<read_array;
binaryFile.seekg(0,ios::beg);
//tellg()//返回输入文件读指针的当前位置;
//seekg(文件中的位置)//将输入文件中的读指针移动到指定位置
//seekg(位移量,参照位置)//以参照位置为基准移动若干字节
//beg//从文件开头计算要移动的字节数
//cur//从文件指针的当前位置计算要移动的字节数
//end//从文件的末尾计算要移动的字节数
//一个回车键占2个字符
//
fstream binaryReadFile("d:\\test.txt",ios::out|ios::binary|ios::in);
binaryReadFile.write(arr,12);
auto length = binaryReadFile.tellg();cout<<"length ----->"<<length<<endl;
char *buff = new char[length];
binaryReadFile.seekg(ios::beg);
static char read_array1[10]; // 在此我将打算读出些数据
binaryReadFile.read(read_array1,3); // 读出前三个字符——"Hel"
cout <<"read_array is ------>" <<read_array1 << endl;
binaryReadFile.read(buff,length);
cout<<"buff is ------>"<<buff<<ends;
binaryReadFile.close();
return 0;
}
写的很乱,基本就是put,get,getline,write,read等的用法,及其中的一些相关参数。仅此记录吧。