p>你可以在C++中进行类似的文件读取, #include <iostream> #include <fstream> #include <string> int main () { string line; ifstream in("transit_test.py"); //open file handler if(in.is_open()) //check if file open { while (!in.eof() ) //until the end of file { getline(in,line); //read each line // do something with the line } in.close(); //close file handler } else { cout << "Can not open file" << endl; } return 0; } #包括 #包括 #包括 int main() { 弦线; ifstream in(“transit_test.py”);//打开文件处理程序 if(in.is_open())//检查文件是否打开 { 而(!in.eof())//直到文件结束 { getline(in,line);//读取每一行 //用绳子做点什么 } in.close();//关闭文件处理程序 } 其他的 { cout,c++,string,file,C++,String,File" /> p>你可以在C++中进行类似的文件读取, #include <iostream> #include <fstream> #include <string> int main () { string line; ifstream in("transit_test.py"); //open file handler if(in.is_open()) //check if file open { while (!in.eof() ) //until the end of file { getline(in,line); //read each line // do something with the line } in.close(); //close file handler } else { cout << "Can not open file" << endl; } return 0; } #包括 #包括 #包括 int main() { 弦线; ifstream in(“transit_test.py”);//打开文件处理程序 if(in.is_open())//检查文件是否打开 { 而(!in.eof())//直到文件结束 { getline(in,line);//读取每一行 //用绳子做点什么 } in.close();//关闭文件处理程序 } 其他的 { cout,c++,string,file,C++,String,File" />

在C++; 作为C++新手,来自Python背景,我尝试将下面的代码翻译成C++ < f = open('transit_test.py') s = f.read() P>什么是最短的C++习语?这样做的?< /P> < P> C++的STL方式是这样做的: #include <string> #include <iterator> #include <fstream> using namespace std; wifstream f(L"transit_test.py"); wstring s(istreambuf_iterator<wchar_t>(f), (istreambuf_iterator<wchar_t>()) ); #包括 #包括 #包括 使用名称空间std; wifstream f(L“transit_test.py”); wstring s(istreambuf_迭代器(f),(istreambuf_迭代器()); > p>你可以在C++中进行类似的文件读取, #include <iostream> #include <fstream> #include <string> int main () { string line; ifstream in("transit_test.py"); //open file handler if(in.is_open()) //check if file open { while (!in.eof() ) //until the end of file { getline(in,line); //read each line // do something with the line } in.close(); //close file handler } else { cout << "Can not open file" << endl; } return 0; } #包括 #包括 #包括 int main() { 弦线; ifstream in(“transit_test.py”);//打开文件处理程序 if(in.is_open())//检查文件是否打开 { 而(!in.eof())//直到文件结束 { getline(in,line);//读取每一行 //用绳子做点什么 } in.close();//关闭文件处理程序 } 其他的 { cout

在C++; 作为C++新手,来自Python背景,我尝试将下面的代码翻译成C++ < f = open('transit_test.py') s = f.read() P>什么是最短的C++习语?这样做的?< /P> < P> C++的STL方式是这样做的: #include <string> #include <iterator> #include <fstream> using namespace std; wifstream f(L"transit_test.py"); wstring s(istreambuf_iterator<wchar_t>(f), (istreambuf_iterator<wchar_t>()) ); #包括 #包括 #包括 使用名称空间std; wifstream f(L“transit_test.py”); wstring s(istreambuf_迭代器(f),(istreambuf_迭代器()); > p>你可以在C++中进行类似的文件读取, #include <iostream> #include <fstream> #include <string> int main () { string line; ifstream in("transit_test.py"); //open file handler if(in.is_open()) //check if file open { while (!in.eof() ) //until the end of file { getline(in,line); //read each line // do something with the line } in.close(); //close file handler } else { cout << "Can not open file" << endl; } return 0; } #包括 #包括 #包括 int main() { 弦线; ifstream in(“transit_test.py”);//打开文件处理程序 if(in.is_open())//检查文件是否打开 { 而(!in.eof())//直到文件结束 { getline(in,line);//读取每一行 //用绳子做点什么 } in.close();//关闭文件处理程序 } 其他的 { cout,c++,string,file,C++,String,File,我很确定我以前发布过这个,但它足够短,可能不值得找到前面的答案: std::ifstream in("transit_test.py"); std::stringstream buffer; buffer << in.rdbuf(); std::ifstream-in(“transit_test.py”); std::stringstream缓冲区; 缓冲区你只是想炫耀一下!:/@JohnB:还是寻求同情?我不认为阅读文件是“炫耀”。是的,它会。不幸的是,STL不提供以任意编码加

我很确定我以前发布过这个,但它足够短,可能不值得找到前面的答案:

std::ifstream in("transit_test.py");
std::stringstream buffer;

buffer << in.rdbuf();
std::ifstream-in(“transit_test.py”);
std::stringstream缓冲区;

缓冲区你只是想炫耀一下!:/@JohnB:还是寻求同情?我不认为阅读文件是“炫耀”。是的,它会。不幸的是,STL不提供以任意编码加载文件的自动方式,因此您需要提前知道文件的编码并相应地调整代码,或者使用库识别编码并执行适当的转换。
rs(T(f),T());
是一个函数声明。@Abyx:
rs(T(f),(T());
不是。;)由于相对较长,这是一个错误:它通常会读取最后一行两次(
),而(!in.eof())
几乎是一个有保证的错误)。永远不要将eof测试置于while条件下。这不会导致冗余副本吗?这又如何。@马丁:是的,但除非我遇到了真正的问题(我还没有遇到),否则我会怀疑试图避免它是否是过早的优化。