在 C++ 中使用 Visual Studio 读取文本文件时遇到问题

Trouble Reading a Text File with Visual Studio in C++

我无法 Visual Studio 读取文本文件。下面是我的代码。该文件在 Unix 环境中可以完美打开,但在复制并粘贴到 Visual Studio 时无法正常工作。我正在使用 fstream 打开文件。为什么文件没有被读取?

当我 运行 程序时,它构建了但我没有得到任何输出。我有一个输出语句cout << "inf\n"。所以甚至没有到达循环,这就是为什么我相信文件没有被读取的原因。同样,当我在 Unix 环境中 运行 相同的代码时,输​​出语句会显示并显示文件中的值( via tree.insert(), tree.remove() )。

我尝试了 this link 中的解决方案。按照它的建议,我将工作目录更改为 $(ProjectDir)\Debug 和 $(ProjectDir)\Release。此外,我将我的文本文件从资源文件夹移动到解决方案资源管理器中的源文件夹。但是,文件仍然没有被读取。

我还更新了我的代码以在 fstream inf ("BTREE5_1.txt") 之后直接包含 cerr << "Error: " << strerror(errno);。使用这行代码,我得到的输出是

Error: No such file or directory

谁能解释一下为什么?如上所述,我的文本文件与我的代码位于同一文件夹中。

#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <cstdlib>  
#include <cstdio>
#include "BTree.h"

using namespace std;

int main()
{
bool first = true;

BTree tree(6, 2);
int value;
char s[80], command;

ifstream inf("BTree5_1.txt");
cerr << "Error: " << strerror(errno);


inf.getline(s, 80);

while (inf >> command >> value)
{
    cout << "inf\n";
    if (command == 'i')
        tree.insert(value);
    else
    {
        if (first)
        {
            cout << "After all insertions.\n";
            tree.print();
            first = false;
        } // if first

        cout << "Deleting " << value << ". \n";
        tree.remove(value);
        tree.print();
        // fgets(s, 80, stdin);
    } // else deletion
} // while
system("PAUSE");
return 0;
}  // main

问题是我从 Unix 环境复制并粘贴了我的文本文件。为了解决这个问题,我只是将文本文件从我的 C 驱动器放入我的目录中。

ie>) C:\Users\s.proctor\Documents\Visual 工作室 2015\Projects\ecs60\p2\p2\p2