使用C+打开文件+;并输出文件中的文本 我试图用C++打开一个文件,并输出文件中的文本。我似乎不知道我做错了什么。这是我到目前为止所拥有的 #include <iostream> #include <string> #include <fstream> #include <cstdlib> using namespace std; int main() { char fileName[50]; ifstream infile; cout << "Enter the name of the file you would like to open: "; cin.getline(fileName, 50); infile.open(fileName); if(!infile.is_open()) { exit(EXIT_FAILURE); } char line[75]; infile >> line; while (infile.good()) { cout << line << " "; infile >> line; } system("pause"); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 字符文件名[50]; 河流充填; 曲线; while(infle.good()) { cout线; } 系统(“暂停”); 返回0; }

使用C+打开文件+;并输出文件中的文本 我试图用C++打开一个文件,并输出文件中的文本。我似乎不知道我做错了什么。这是我到目前为止所拥有的 #include <iostream> #include <string> #include <fstream> #include <cstdlib> using namespace std; int main() { char fileName[50]; ifstream infile; cout << "Enter the name of the file you would like to open: "; cin.getline(fileName, 50); infile.open(fileName); if(!infile.is_open()) { exit(EXIT_FAILURE); } char line[75]; infile >> line; while (infile.good()) { cout << line << " "; infile >> line; } system("pause"); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 字符文件名[50]; 河流充填; 曲线; while(infle.good()) { cout线; } 系统(“暂停”); 返回0; },c++,C++,输入文件名并按enter键后,CMD提示符将关闭。我知道该文件存在,但我无法找出它退出的原因。显然这是因为exit命令,但它应该是打开的。我做错了什么?没有必要使用char[]。你甚至有#includedstring,所以就用它吧 string fileName; cout << "Enter the name of the file you would like to open: "; cin >> fileName; // or // getline(cin, fil

输入文件名并按enter键后,CMD提示符将关闭。我知道该文件存在,但我无法找出它退出的原因。显然这是因为exit命令,但它应该是打开的。我做错了什么?

没有必要使用
char[]
。你甚至有
#include
d
string
,所以就用它吧

string fileName;
cout << "Enter the name of the file you would like to open: ";
cin >> fileName;
// or
// getline(cin, fileName);

ifstream infile(fileName);

if (infile.fail()) {
    exit(EXIT_FAILURE);
}

string line;

while (infile >> line) {
    cout << line << " ";
}

system("pause");
return 0;
字符串文件名;
cout>文件名;
//或
//getline(cin,文件名);
ifstream-infle(文件名);
if(infle.fail()){
退出(退出失败);
}
弦线;
而(填充>>行){

CUT< P>不需要逐行读取/写入文件;C++已经支持在一步中复制文件。您也应该使用String < /C> >而不是使用< Calp>chAR[]/Cord>字符串,一方面意味着不需要将字符串的最大长度限制为任意长度。(如果文件的路径名超过50个字符,或者文件的行超过75个字符,该怎么办

还要注意,文件复制代码是错误的:它将删除文件中的所有空白,因为
infle>>line
不读取一行(使用readline),而是读取一个单词,丢弃空白

此外,如果代码无法打开文件,则应该给出错误消息,而不是静默返回(您确实提供了错误返回,这很好,但除非您从实际提供错误返回反馈的内容调用它,否则您永远不会了解它

最后,
系统(“暂停”)
可能应该在RAII类中完成,因此它保证在返回时退出(但是,
exit
不会调用析构函数,因此除非您想使用
atexit
,否则应该在`main``中使用
return
)。不过,更好的办法是不要将其放入代码中,而是在程序完成后不会立即关闭的终端中运行

下面是一个实现这些建议的程序:

#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{
  // make sure that system("pause") is called on all exit paths
  struct cleanup
  {
    ~cleanup() { std::system("pause"); }
  } do_cleanup;

  // get the file name
  std::string filename;
  std::cout << "Enter the name of the file you would like to open: ";
  std::getline(std::cin,filename);
  if (!std::cin)
  {
    std::cerr << "Failed to read the file name.\n";
    return EXIT_FAILURE;
  }

  // open the file
  std::ifstream infile(filename.c_str());
  if (!infile)
  {
    std::cerr << "Could not open file: " << filename << "\n";
    return EXIT_FAILURE;
  }

  // print the file
  std::cout << infile.rdbuf();

  // close the file
  infile.close();
  if (!infile)
  {
    std::cerr << "Could not properly close file: " << filename << "\n";
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
#包括
#包括
#包括
int main()
{
//确保在所有退出路径上调用系统(“暂停”)
结构清理
{
~cleanup(){std::system(“暂停”);}
}做清洁工作;
//获取文件名
std::字符串文件名;

STD::谢谢你的帮助。是的,文件在错误的文件夹中。这是Neb B的疏忽。< /P>尝试进入绝对路径名。你的开发环境可能是执行一个不同于你认为的目录的代码。此外,使用<代码> STD::String < /Cord>代替字符数组。这是C++,而不是C。使用Visual Studio(假设您正在执行
系统(“暂停”)
。我建议使用调试器逐步检查代码以查看问题所在。我仍然得到完全相同的结果。窗口立即关闭。我怀疑您的文件位于错误的文件夹中。它是否与项目文件位于同一文件夹中?如果文件名中有空格,则代码将失败。使用getline读取它是一种简单的方法g在原始代码中正确完成。