#include<iostream>
using namespace std;
#include <fstream>//頭文件包含
//文本文件 寫文件
void test01()
{
?? ?//1.包含頭文件? fstream
?? ?//2.創(chuàng)建流對(duì)象
?? ?ofstream ofs;
?? ?//3.指定打開方式
?? ?ofs.open("test.txt", ios::out);
?? ?//4.寫內(nèi)容
?? ?ofs << "姓名:張三" << endl;
?? ?ofs << "性別:男" << endl;
?? ?ofs << "年齡:18" << endl;
?? ?//5.關(guān)閉文件
?? ?ofs.close();
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
總結(jié):
* 文件操作必須包含頭文件 fstream
* 寫文件可以利用 ofstream? ,或者fstream類
* 打開文件時(shí)候需要指定操作文件的路徑,以及打開方式
* 利用<<可以向文件中寫數(shù)據(jù)
* 操作完畢,要關(guān)閉文件
#include<iostream>
using namespace std;
#include <string>
#include <fstream>//頭文件包含
//文本文件? 讀文件
void test01()
{
?? ?//1.包含頭文件
?? ?//2.創(chuàng)建流對(duì)象
?? ?ifstream ifs;
?? ?//3.打開文件? 并且判斷是否打開成功
?? ?ifs.open("test.txt", ios::in);
?? ?if (!ifs.is_open())
?? ?{
?? ??? ?cout << "文件打開失敗" << endl;
?? ??? ?return;
?? ?}
?? ?//4.讀數(shù)據(jù)
?? ?//第一種方式
?? ?//char buf[1024] = { 0 };
?? ?//while (ifs >> buf)
?? ?//{
?? ?//?? ?cout << buf << endl;
?? ?//}
?? ?//第二種
?? ?//char buf[1024] = { 0 };
?? ?//while (ifs.getline(buf,sizeof(buf)))
?? ?//{
?? ?//?? ?cout << buf << endl;
?? ?//}
?? ?//第三種
?? ?//string buf;
?? ?//while (getline(ifs, buf))
?? ?//{
?? ?//?? ?cout << buf << endl;
?? ?//}
?? ?//第四種
?? ?char c;
?? ?while ((c = ifs.get()) != EOF)//EOF end of file文件尾部的標(biāo)準(zhǔn)
?? ?{
?? ??? ?cout << c;
?? ?}
?? ?//5.關(guān)閉文件
?? ?ifs.close();
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-830125.html
總結(jié):
- 讀文件可以利用 ifstream? ,或者fstream類
- 利用is_open函數(shù)可以判斷文件是否打開成功
- close 關(guān)閉文件
#include<iostream>
using namespace std;
#include <string>
#include <fstream>//頭文件包含
//二進(jìn)制文件? 寫文件
class Person
{
public:
?? ?char m_Name[64];//姓名
?? ?int m_Age;//年齡
};
void test01()
{
?? ?//1、包含頭文件
?? ?//2、創(chuàng)建流對(duì)象
?? ?ofstream ofs("person.txt", ios::out | ios::binary);
?? ?
?? ?//3、打開文件
?? ?//ofs.open("person.txt", ios::out | ios::binary);
?? ?//4、寫文件
?? ?Person p = {"張三"? , 18};
?? ?ofs.write((const char *)&p, sizeof(p));
?? ?//5、關(guān)閉文件
?? ?ofs.close();
}
int main()
{
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
總結(jié):
* 文件輸出流對(duì)象 可以通過(guò)write函數(shù),以二進(jìn)制方式寫數(shù)據(jù)
#include<iostream>
using namespace std;
#include <string>
#include <fstream>//頭文件包含
//二進(jìn)制文件? 讀文件
class Person
{
public:
?? ?char m_Name[64];//姓名
?? ?int m_Age;//年齡
};
void test01()
{
?? ?//1.包含頭文件
?? ?//2.創(chuàng)建流對(duì)象
?? ?ifstream ifs("person.txt", ios::in | ios::binary);
?? ?//3.打開文件? 判斷文件是否打開成功
?? ?if (!ifs.is_open())
?? ?{
?? ??? ?cout << "文件打開失敗" << endl;
?? ?}
?? ?//4.讀文件
?? ?Person p;
?? ?ifs.read((char *)&p, sizeof(p));
?? ?cout << "姓名: " << p.m_Name << " 年齡: " << p.m_Age << endl;
?? ?//5.關(guān)閉文件
?? ?ifs.close();
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
總結(jié)- 文件輸入流對(duì)象 可以通過(guò)read函數(shù),以二進(jìn)制方式讀數(shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830125.html
到了這里,關(guān)于C++文件操作->文本文件(->寫文件、讀文件)、二進(jìn)制文件(->寫文件、讀文件)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!