c++涓璮ile濡備綍鏀捐繘鏁扮粍
鍦–++涓紝鍙互灏嗘枃浠舵斁鍏ユ暟缁勫彉閲忎腑锛屼絾鏄渶瑕佸厛璇诲彇鏂囦欢鍐呭锛屽苟灏嗗唴瀹瑰瓨鍌ㄥ埌鏁扮粍涓€備笅闈㈡槸涓€涓ず渚嬩唬鐮侊細
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream file("example.txt");
std::string line;
std::vector<std::string> lines;
if (file.is_open()) {
while (std::getline(file, line)) {
lines.push_back(line);
}
file.close();
// 灏嗘枃浠跺唴瀹瑰瓨鍌ㄥ埌鏁扮粍涓?/span>
for (const auto& l : lines) {
std::cout << l << std::endl;
}
} else {
std::cout << "Unable to open file" << std::endl;
}
return 0;
}
涓婇潰鐨勪唬鐮佺ず渚嬫墦寮€涓€涓悕涓?quot;example.txt"鐨勬枃浠跺苟浠庝腑璇诲彇鍐呭锛岀劧鍚庡皢姣忎竴琛屽瓨鍌ㄥ埌涓€涓獀ector涓€傛渶鍚庡皢鏂囦欢鍐呭鎵撳嵃鍑烘潵銆傛偍鍙互鏍规嵁闇€瑕佷慨鏀逛唬鐮佹潵澶勭悊鏂囦欢鍐呭銆?/p>