在Xcode中遇到fstream的问题 [英] Having trouble with fstream in Xcode

查看:286
本文介绍了在Xcode中遇到fstream的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证REGISTER.txt的存在时遇到问题,无法在函数中输入(参见下文)。我的理解是,如果文件不存在,那么该文件将不会被打开,文件流变量(inData)将为false。因此,我可以在if / else语句中使用该变量来验证它是否打开。但是,即使REGISTER.txt与我的.cpp文件在同一个目录中,我的代码仍然表示它没有打开。

I'm having trouble validating the existence of REGISTER.txt for input purposes in a function (see below). My understanding is that if the file doesn't exist, then the file won't be opened and the file stream variable (inData) will be false. Thus, I can use that variable in an if/else statement to verify whether or not it opened. But even though REGISTER.txt is in the same directory as my .cpp file, my code still says that it wasn't opened.

这里的东西。当我在Dev-C ++编译器中运行相同的确切代码,它工作正常,找到文件。现在,我明白编译器是不同的,但我不明白是什么导致这里的差异。我首选的IDE是Xcode,所以我想学习如何用Xcode中的文件来做I / O。

Here's the thing though. When I run the same exact code in Dev-C++ compiler, it works fine and the file is found. Now, I understand compilers are different, but I don't understand what is causing the discrepancy here. My preferred IDE is Xcode, so I'd like to learn how to do I/O with files in Xcode.

先感谢您的帮助。

PS我的Xcode项目引用文件,所以它不像项目不与文件连接。

P.S. My Xcode project references the file, so it's not like the project isn't connected with the file.

void ReadVehicleRegInfo(char& vehicleType, string& licensePlate,
                    int& modelYear, float& origTaxValue, bool& error)
{
    ifstream inData;

    string inputFile = "REGISTER.txt";
    inData.open(inputFile.c_str());        //File contains registration info

    if (!inData) {
        //File does not exist. Exit function
        cout << inputFile << " does not exist. Program will now terminate"
             << endl << endl;
        error = true;

        return;
    } else {
        //File exists - continue with program
        cout << inputFile << " found";
    }

    inData.close();
}



在我的main()函数中,我有以下代码用户已发生错误:

In my main() function, I have the following code to signal to the user that an error has occurred:

if (error) {
    //Function encountered error. Exits program
    system("PAUSE");
    return 99;
}

EDIT b $ b我花了40分钟试图弄清楚这一点,15写的问题,和5分钟后我发布我取得巨大的进步。你不爱那个吗?

EDIT I spent 40 minutes trying to figure this out, 15 writing the question, and 5 minutes after I post it I make huge progress. Don't you love that?

我把完整的目录放到文件,这样做的伎俩。

I put in the full directory to the file and that did the trick.

但是,这并不理想。下一个问题是,如何避免这样做? Xcode的默认目录是什么?

However, this is not ideal. The next question is how do I avoid having to do that? What is the default directory for Xcode?

推荐答案


REGISTER.txt与我的目录.cpp文件

REGISTER.txt is in the same directory as my .cpp file

REGISTER.txt需要与二进制文件位于同一目录中(build / Release或其他位置,具体取决于您的构建设置)

REGISTER.txt needs to be in the same directory as the binary (build/Release or elsewhere depending on your build settings)

这篇关于在Xcode中遇到fstream的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆