0

我正在开发一个项目,该项目要求必须从文件中获取输入然后进行处理。我们获得了用于打开文件并检查以确保它已打开的代码框架,但我在打开文件时遇到问题。

我的代码

int main()

{
    readFile();
}

void readFile()
{
    ifstream fin;
    string fileName;
    int x;

    cout << "Enter the file you wish to read from: ";
    cin >> fileName;

    fin.open(fileName.c_str(), ios::in);

    if(!fin.is_open())
    {
        cerr << "Unable to open file " << fileName << endl;
        exit(10);
    }
}

我的控制台:http: //i.stack.imgur.com/2derI.jpg

我们基本上得到了所有这些代码,所以我完全不知道为什么这不起作用。这只是我更大项目的一部分,但在我开始工作之前,我什至无法尝试这部分。任何帮助将非常感激。

4

2 回答 2

0

可能您的目录设置不显示文件扩展名。您需要输入12.txt.txt而不是12.txt.

于 2012-12-01T23:36:47.823 回答
0

I believe it may have to do with the file name, I compiled and ran this code fine when choosing a different file, but I made a 12.txt file and it produced the same error. It is some issue with the file name being a number I believe, I would suggest renaming the file, though I don't know what makes this occur

于 2012-12-02T00:42:21.473 回答