Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 输入文件问题:Input_File.fail()为true,idk为什么_C++_File - Fatal编程技术网

C++ 输入文件问题:Input_File.fail()为true,idk为什么

C++ 输入文件问题:Input_File.fail()为true,idk为什么,c++,file,C++,File,我的代码应该打开一个文件并处理它读入的数据。我的代码如下所示: #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { ifstream input_file; input_file.open("practice.txt"); if (input_file.fail()) { cout << "

我的代码应该打开一个文件并处理它读入的数据。我的代码如下所示:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
int main()
{
  ifstream input_file;
  input_file.open("practice.txt");

  if (input_file.fail())
  {
    cout << "Attempt to open file failed." << endl;
  }

  else
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入文件;
输入_file.open(“practice.txt”);
if(input_file.fail())
{

可能最有可能的原因是程序运行的目录与文件所在的目录不同。请使用文件的绝对路径,以确保权限不是打开文件失败的原因。搜索词:“工作目录”@RSahu我该怎么做?我使用Code::Blocks作为编译器,它在我的计算机上的文档中创建了一个文件夹,因此我将该文件放在它为该项目创建的文件夹中。最好放在哪里?假设您使用的是Windows,请将输入文件practice.txt移到“C:\”。将打开文件的行更改为
input\u file。打开(“C:\\practice.txt”)
。请注意使用两个反斜杠-“\\”。不要将其更改为一个。我使用的是Mac。您熟悉这方面的任何等价物吗?最可能的原因是程序运行的目录与文件所在的目录不同。请使用文件的绝对路径,以确保权限不对打开文件的失败负责。搜索词:“Working Directory”@RSahu我该怎么做?我使用Code::Blocks作为编译器,它在我的计算机上的文档中创建了一个文件夹,因此我将该文件放在它为该项目创建的文件夹中。最好放在哪里?假设您使用的是Windows,请将输入文件practice.txt移到“C:\“。更改行以将文件打开为
input\u file。打开(“C:\\practice.txt”)
。请注意使用两个反斜杠-“\\”。不要将其更改为一个。我使用的是Mac。您熟悉这方面的等效物吗?