C++ 读取文件的四种方式
生活随笔
收集整理的這篇文章主要介紹了
C++ 读取文件的四种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ?本文介紹C++ 讀取文件的四種方式。
? ? 第一種方式:
#include<iostream> #include<fstream> using namespace std;int main() {ifstream ifs;ifs.open("text.txt",ios::in);if (!ifs.is_open()){cout << "read fail." << endl;return -1;}char buf[1000] = { 0 };while (ifs >> buf){cout << buf << endl;}getchar();return 0; }? ? 第二種方式:
char buf[1000] = { 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) {cout << c; }?
總結
以上是生活随笔為你收集整理的C++ 读取文件的四种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3步实现财务分析数据可视化,财务人必备
- 下一篇: 弹性系数系数在水文气象中的应用