文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
1.ofstream,open,close 寫入文件
#include<iostream>
#include<fstream>
?
using namespace std;
?
//通過ofstream的方式實現寫入文件 open,close
void main()
{
??? ofstream fout;? //ofstream輸出文件
??? fout.open("E:\\1.txt");//打開文件
??? fout << "1234abcdef";//寫入文件
??? fout.close();
}
通過這些代碼向文件1.txt中輸入文件
2.ifstream,fin 從文件中讀取文件并打印輸出到屏幕
#include<iostream>
#include<fstream>
?
using namespace std;
?
//通過ifstream流讀取文件,并將文件寫入str中
void main()
{
??? ifstream fin("E:\\1.txt");//創建讀取文件的流
??? char str[50] = { 0 };
??? fin >> str;//讀取
??? fin.close();
??? cout << str;
??? cin.get();
}
運行結果是,輸出:1234abcdef
3.按照行來讀取數據
#include<iostream>
#include<fstream>
?
using namespace std;
?
//按照行來讀取
void main()
{
??? //按照行來讀取
??? ifstream fin("E:\\1.txt");
??? //讀取4行數據
??? for (int i = 0; i < 4;i++)
??? {
??????? char str[50] = { 0 };
??????? fin.getline(str, 50);
??????? cout << str << endl;
??? }
??? fin.close();
??? cin.get();
}
上面結果是輸出4行。
4.fout文件輸入
#include<iostream>
#include<fstream>
?
using namespace std;
?
void main()
{
??? ofstream fout;//ofstream.輸出文件
??? fout.open("E:\\2.txt");//打開文件
??? fout << "鋤禾日當午" << endl;//寫入文件
??? fout << "地雷買下土" << endl;//寫入文件
??? fout << "譚勝來跳舞" << endl;//寫入文件
??? fout << "炸成250" << endl;//寫入文件
??? fout.close();
}
5. fstream,ios::in | ios::out表示有讀寫的權限,通過fstream.getline(寫入位置,寫入長度)。
案例(寫入文件又讀取文件的方式):
#include<iostream>
#include<fstream>
?
using namespace std;
?
//通過fstream的方式實現文件讀寫,要注意的是這種方式要求文件已經存在
void main()
{
??? //ios::in | ios::out表示有讀寫的權限
??? fstream fio("E:\\3.txt", ios::in | ios::out);
??? fio << "鋤禾日當午" << endl;//寫入文件
??? fio << "地雷買下土" << endl;//寫入文件
??? fio << "譚勝來跳舞" << endl;//寫入文件
??? fio << "炸成250" << endl;//寫入文件
??? fio.close();
??? {
??????? fstream fio("E:\\3.txt", ios::in | ios::out);
??????? for (int i = 0; i < 4; i++)
??????? {
??????????? char str[50] = { 0 };
??????????? fio.getline(str, 50);
??????????? cout << str << endl;
??????? }
??????? fio.close();
??? }
??? cin.get();
}
6.fio.seekg();隨機文件指針,將文件指針移動到指定位置開始讀寫文件
案例如下:
#include<iostream>
#include<fstream>
?
using namespace std;
?
//通過fstream的方式實現文件讀寫,要注意的是這種方式要求文件已經存在
void main()
{
??? //ios::in | ios::out表示有讀寫的權限
??? fstream fio("E:\\3.txt", ios::in | ios::out);
??? fio << "鋤禾日當午" << endl;//寫入文件
??? fio << "地雷買下土" << endl;//寫入文件
??? fio << "譚勝來跳舞" << endl;//寫入文件
??? fio << "炸成250" << endl;//寫入文件
??? fio.close();
??? fio.seekg(ios::beg); //文件指針,從文件開頭開始執行
??? {
??????? fstream fio("E:\\3.txt", ios::in | ios::out);
??????? for (int i = 0; i < 4; i++)
??????? {
??????????? char str[50] = { 0 };
??????????? fio.getline(str, 50);
??????????? cout << str << endl;
??????? }
??????? fio.close();
??? }
??? cin.get();
}
7.文件寫入和文件讀寫
#include<iostream>
#include<fstream>
?
using namespace std;
?
void main()
{
??? ofstream fout;
??? fout.open("E:\\4.txt");
??? //將內容打印到文件
??? fout << "ABC" << " " << 123 << " " << 'ch' << endl;
??? fout.close();
??? ifstream fin("E:\\4.txt");
??? char str[10] = { 0 };//讀取字符串
??? int num = 0;
??? char ch = '\0';
??? fin >> str >> num >> ch;
??? std::cout << str << "\n" << num << "\n" << ch;
?
??? std::cin.get();
}
運行結果:
ABC
123
8.文件拷貝
#include<iostream>
#include<fstream>
?
using namespace std;
?
//讀寫一個字符
//文本與二進制存儲
void main()
{
??? ifstream fin("E:\\4.txt");//創建讀取文件的流
??? ofstream fout("E:\\40.txt");
??? if (!fin || !fout)
??? {
??????? std::cout << "文件打開失敗";
??????? return;
??? }
??? std::cout << "文件拷貝開始\n";
??? char ch = 0;
??? //引用的方法讀取到一個字符
??? while (fout && fin.get(ch))
??? {
??????? fout.put(ch);//寫入一個字節
??? }
??? fin.close();
??? fout.close();
?
??? std::cout << "文件拷貝完成";
??? cin.get();
}
運行結果:生成了一個40.txt,里面存儲的是4.txt中的內容
9.文件追加ios:app
#include<iostream>
#include<fstream>
?
using namespace std;
?
void main()
{
??? ofstream fout("E:\\40.txt", ios::app);//追加
??? fout << "天下英雄,譚勝第一\n";
??? fout.close();
?
??? cin.get();
}
發現在40.txt這個文件的最后面多了:天下英雄,譚勝第一
?
?
總結
以上是生活随笔為你收集整理的文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高斯贝尔遭调查最新消息 开盘一字跌停报
- 下一篇: 支付宝余额提现怎么免手续费