當(dāng)前位置:
首頁 >
python 打开文件,读取文件内容
發(fā)布時間:2025/3/15
22
豆豆
生活随笔
收集整理的這篇文章主要介紹了
python 打开文件,读取文件内容
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include <iostream>
#include <fstream>
using namespace std;int main()
{int x,sum=0;ifstream srcFile("result.txt", ios::in); //以文本模式打開in.txt備讀if (!srcFile) { //打開失敗cout << "error opening source file." << endl;return 0;}ofstream destFile("out.txt", ios::out); //以文本模式打開out.txt備寫if (!destFile) {srcFile.close(); //程序結(jié)束前不能忘記關(guān)閉以前打開過的文件cout << "error opening destination file." << endl;return 0;}//可以像用cin那樣用ifstream對象while (srcFile >> x) {sum += x;//可以像 cout 那樣使用 ofstream 對象destFile << x << " ";}cout << "sum:" << sum << endl;destFile.close();srcFile.close();return 0;
}
文件里內(nèi)容就是上述的數(shù)字,并且進行了求和運算。通過上述腳本可以簡單的實現(xiàn),讀取文件內(nèi)容的功能。
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python 打开文件,读取文件内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bulk API实现批量操作
- 下一篇: python关于字符串下面说法错误的是_