C++?????????????????????????
?????????? ???????[ 2016/9/12 14:37:42 ] ??????????????????? C++
????5?????I/O
????????????豸???????????????????????????????豸????????cout??????????????????????????????????????????????
????ifstream???????istream?????????????????????????????
????ofstream???????ostream???????????????????????????????
????fstream???????iostream??????????????????????????????????
??????????
??????????????????????????????λ????I/O??????????ζ?д?????????????λ?????????????????????????????????????????????????????????EOF????????????????????????-1)??????????????????eof??????0?(??????1)???????????? ???
??????????????????????????????????????????????????????????Ч????????????????????????????????????????
????????????
????1??ASCII??????????????????о???ASCII?????????????????????????????????????????ASCII???(?????????)??
????2?????????????????е????????????????????????е???????????????????????????????
???????ú???
???????????
???????1??????????????????????????ofstream?????????????
????????? ofstream (??????????? ??????????);
???????ofstream ?0(??)??????????????
?????磺 ofstream fout1(fname??ios::out);
??????????????????????????????????????“??”????“|”?????????磺fstream fout(fname??ios::out|ios::in)
???????2????????????????????????????????????????open?????????
????????? ?????????.open(??????????? ??????????);
????????????0(??)???????????
?????磺 fout.open(fname??ios::out)
????????????
?????????????????????д???????????????????? ?磺 outfile.close( );
???????????
????1???????????????????“<<”????????????“>>”????????????????????>> ???????????????ж?????????
????2????????????????put??get??geiline??????????????????????????
#include <iostream>
using namespace std;
#include "fstream"
int main()
{
char fname[] = "d:/file1.txt";
char buff[1024]= {0};
/*********** д??? *************/
//???1 ?????ofstream???????fopen????
ofstream fout;
fout.open(fname??ios::out);
if(!fout)
{
cout<<"????????"<<fname<<endl;
}
fout<< "hello world !"; //????????????д???????
fout.flush();
fout.close();
//???2 ?????????ofstream??????????
ofstream fout1(fname??ios::out);
if(!fout1)
{
cout<<"????????"<<fname<<endl;
}
fout1.put('h'); //???put????д?????
fout1.put('e');
fout1.put('l');
fout1.put('l');
fout1.put('o');
fout1.put('
');
fout1.flush();
fout1.close();
//?????????д???
fstream file2(fname??ios::in|ios::out);
file2<<"abdfd
";
file2<<"11111
";
file2.flush();
file2.close();
/*********** ????? *************/
//??????ifstream?????????????
ifstream fin;
fin.open(fname??ios::in);
fin.getline(buff??1024); //???getline????????????
cout<<buff<<endl;
fin.close();
//?????????????????
fstream file1(fname??ios::in|ios::out);
file1>>buff; //?????????????????????
file1.close();
cout<<buff<<endl;
system("pause");
return 0;
}
?????????????????
?????????????????д?????istream?????????read??write?????????????????????????
#include <iostream>
using namespace std;
#include <fstream>
class Teacher
{
public:
Teacher()
{
}
Teacher(int age??char name[20])
{
this->age = age;
strcpy(this->name??name);
}
void prinfInfo()
{
cout<<"Teacher name:"<<this->name<<" age:"<<this->age<<endl;
}
private:
int age;
char name[20];
};
int main()
{
Teacher t1(31??"xiaoming");
Teacher t2(32??"xiaohong");
Teacher t3(33??"xiaohua");
Teacher t4(34??"xiaoxin");
char fname[] = "d:/file2";
fstream fs(fname??ios::binary|ios::out);
if(!fs)
{
cout<<"????????"<<endl;
}
fs.write((char *)&t1??sizeof(Teacher));
fs.write((char *)&t2??sizeof(Teacher));
fs.write((char *)&t3??sizeof(Teacher));
fs.write((char *)&t4??sizeof(Teacher));
fs.flush();
fs.close();
fstream fs2(fname??ios::binary|ios::in);
if(!fs)
{
cout<<"????????"<<endl;
}
Teacher tt;
fs2.read((char *)&tt??sizeof(Teacher));
tt.prinfInfo();
fs2.read((char *)&tt??sizeof(Teacher));
tt.prinfInfo();
fs2.read((char *)&tt??sizeof(Teacher));
tt.prinfInfo();
fs2.read((char *)&tt??sizeof(Teacher));
tt.prinfInfo();
fs2.close();
system("pause");
return 0;
}
?????????
????Teacher name:xiaoming age:31
????Teacher name:xiaohong age:32
????Teacher name:xiaohua age:33
????Teacher name:xiaoxin age:34
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11