Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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++;ifstream XCode/VSCode 我是C++初学者,我试着逐行地打开和读取文件,但是在xSCODE中似乎不起作用,而在VSCODE中工作:_C++_Xcode_Ifstream - Fatal编程技术网

C++;ifstream XCode/VSCode 我是C++初学者,我试着逐行地打开和读取文件,但是在xSCODE中似乎不起作用,而在VSCODE中工作:

C++;ifstream XCode/VSCode 我是C++初学者,我试着逐行地打开和读取文件,但是在xSCODE中似乎不起作用,而在VSCODE中工作:,c++,xcode,ifstream,C++,Xcode,Ifstream,守则: #include <iostream> // Imported to read #include <fstream> // Imported to read #include <string> // Imported to write lines #include <vector> // Imported to store lines #include <cassert> // Import to Test for

守则:

#include <iostream> // Imported to read
#include <fstream>  // Imported to read
#include <string>   // Imported to write lines
#include <vector>   // Imported to store lines
#include <cassert>  // Import to Test for IntList

using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;

int main(int argc, const char *argv[])
{
    /* ----------------------- READ INNPUT -----------------------*/
    cout << "----------------------- READ INPUT -----------------------" << endl;

    vector<string> lines; // to store the lines of the text file
    string line;          // <--- each new line to be read of the file

    ifstream file("input_text_file.txt"); // open the file

    int lineNumber = 1;
    if (file) // same as: if (myfile.good())
    {
        while (getline(file, line))
        {
            lines.push_back(line);
            cout << "line: " << lineNumber << " " << line << endl;
            lineNumber++;
        }
    }
    else
    {
        cout << "File not ok " << endl;
    }

    return 0;
}

在XCode中,我有:

----------------------- READ INPUT -----------------------
File not ok 
Program ended with exit code: 0
我的文件夹如下所示:文本文件似乎是XCode项目的一部分

这两个文件也位于同一文件夹中:


您是否知道是什么原因导致此问题,以及如何解决此问题?

您的代码假设程序运行时,
input\u text\u file.txt
位于当前工作目录中。在Xcode中,情况显然不是这样。最简单的修复方法可能是使用一个完全限定的路径名,
/path/to/your/file
当程序运行时,您的代码假定
input\u text\u file.txt
位于当前工作目录中。在Xcode中,情况显然不是这样。最简单的修复方法可能是使用完全限定的路径名,
/path/to/your/file

您的文件可能位于错误的文件夹中,或者名称与您键入的不同。我不确定Xcode下的正确工作目录是什么——这两个文件在同一个文件夹中。似乎XCode找不到它的原因我不明白@PaulSanders你是对的,添加完整的路径解决了它。如果你添加它作为一个答案,我会验证它。谢谢你的帮助,我绝对不会想到这一点。我根据你的要求添加了答案。你的文件可能位于错误的文件夹中,或者名称与你键入的不同。我不确定Xcode下的正确工作目录是什么——这两个文件在同一个文件夹中。似乎XCode找不到它的原因我不明白@PaulSanders你是对的,添加完整的路径解决了它。如果你添加它作为一个答案,我会验证它。谢谢你的帮助,我绝对不会想到这一点。我根据你的要求添加了一个答案。
----------------------- READ INPUT -----------------------
File not ok 
Program ended with exit code: 0