C++ 如何告诉您的程序要读取哪个文件C++; #包括 #包括 #包括 使用名称空间std; 无效读取(); int main(){ read(); 返回0; } 无效读取(){ 字符串文件(“”); 字符串名称文件(“”); cin>>文件名称; ifstream in(文件名); 而(!in.eof()){ getline(在文件中); 难道你必须改变吗 #include <iostream> #include <string> #include <fstream> using namespace std; void read(); int main() { read(); return 0; } void read () { string file(""); string nameOfFile(""); cin >> nameOfFile; ifstream in (nameOfFile); while ( !in.eof() ) { getline(in, file); cout << file; cout << endl; } cout << file; in.close(); }
与C++ 如何告诉您的程序要读取哪个文件C++; #包括 #包括 #包括 使用名称空间std; 无效读取(); int main(){ read(); 返回0; } 无效读取(){ 字符串文件(“”); 字符串名称文件(“”); cin>>文件名称; ifstream in(文件名); 而(!in.eof()){ getline(在文件中); 难道你必须改变吗 #include <iostream> #include <string> #include <fstream> using namespace std; void read(); int main() { read(); return 0; } void read () { string file(""); string nameOfFile(""); cin >> nameOfFile; ifstream in (nameOfFile); while ( !in.eof() ) { getline(in, file); cout << file; cout << endl; } cout << file; in.close(); },c++,C++,与 由于ifstream的默认构造函数不接受std::string作为参数,因此它需要一个char*。因此,使用函数std::string::c_str()将std::string转换为char*您需要在.open()中使用gn打开ifstream,并在文件不存在的情况下使用hendle。函数如下: ifstream in (nameOfFile.c_str()); void read(){ 字符串文件(“”); 字符串fileContent=“”; 字符串名称文件(“”); cin>>文件名
由于
ifstream
的默认构造函数不接受std::string
作为参数,因此它需要一个char*
。因此,使用函数std::string::c_str()
将std::string
转换为char*
您需要在.open()中使用gn打开ifstream
,并在文件不存在的情况下使用hendle。函数如下:
ifstream in (nameOfFile.c_str());
void read(){
字符串文件(“”);
字符串fileContent=“”;
字符串名称文件(“”);
cin>>文件名称;
ifstream-in(nameOfFile.c_str());
打开(nameOfFile,ios::in);
如果(在){
而(!in.eof()){
getline(在文件中);
fileContent+=文件;
}
不能一点反馈:
void read() {
string file("");
string fileContent = "";
string nameOfFile("");
cin >> nameOfFile;
ifstream in(nameOfFile.c_str());
in.open(nameOfFile, ios::in);
if (in){
while (!in.eof()) {
getline(in, file);
fileContent += file;
}
cout << fileContent;
in.close();
}
else {
cout << "Could not open file.";
}
}
void read(){
字符串文件(“”;//您不需要(“”)位;默认为空,
//“文件”是一个可怕的标识符选择
//它听起来更像一个ifstream而不是字符串
//用于保存文件中的一行。
//对此,我倾向于使用“字符串行”。
字符串名称文件(“”;//同上
cin>>nameOfFile;//您应该测试输入是否成功,如下所示:
//如果(!cin>>文件名){
//标准::cerr先不要做,而(!in.eof)-这一秒有这么多帖子-出了什么问题?当你用调试器运行它时,你看到了什么?“In”没有在这个范围内声明,你可以在C++11中使用std::string
。@Kylemcarthy,如果你能告诉我我的答案中缺少了什么?非常感谢你,我从没想到这个社区会如此友好。这段代码没有编译,这是没有回答OP的问题。@Shaurya,我正在使用VS 2013,它在我的机器上运行良好。也许你有其他版本的VS,或者问题出在其他地方。OP有一个in未在此范围内声明错误。也就是说,构造函数参数是错误的,因为ifstream的构造函数采用了字符*
fstd::string
。从char*
到std::string
的类型转换在VS2013中针对ifstream
自动进行。我尝试使用添加.c_str()
函数的解决方案对其进行更改。但在输出中没有发现任何差异。
void read() {
string file("");
string fileContent = "";
string nameOfFile("");
cin >> nameOfFile;
ifstream in(nameOfFile.c_str());
in.open(nameOfFile, ios::in);
if (in){
while (!in.eof()) {
getline(in, file);
fileContent += file;
}
cout << fileContent;
in.close();
}
else {
cout << "Could not open file.";
}
}
void read () {
string file(""); // you don't need the ("") bit; empty by default,
// and "file" is a terrible choice of identifier as
// it sounds more like an ifstream than a string
// used to hold one line from the file.
// I tend to use "string line;" for this.
string nameOfFile(""); // ditto
cin >> nameOfFile; // you should test for success of input, like this:
// if (!cin >> nameOfFile) {
// std::cerr << "error reading filename from stdin\n";
// exit(1);
// }
ifstream in (nameOfFile); // test for success getting file open like this:
// if (ifstream in(nameofFile))
// {
while ( !in.eof() ) { // NEVER check eof before attempting input, instead:
getline(in, file); // while (getline(in, file))
cout << file; // cout << file << endl; // can "chain"
cout << endl; // }
// else
// std::cerr << "couldn't open " << nameOfFile
// << '\n';
} // no need for extra cout nor explicit close, as
cout << file; // the ifstream destructor closes anyway.
in.close();
}