简体   繁体   English

VS2017 C ++ ifstream错误

[英]VS2017 c++ ifstream error

I've just installed Visual Studio to start writing C++ there, I spent the last year learning in CodeBlocks and everything was going fine. 我刚刚安装了Visual Studio,开始在那儿编写C ++,去年我在CodeBlocks中学习,并且一切都很好。 But when I got to trying out the ifstream function, it shoots out an error and I've looked around and can't seem to find an answer anywhere. 但是,当我不得不尝试ifs​​tream函数时,它会抛出一个错误,我四处张望,似乎找不到任何答案。 Could you guys please help? 你们能帮忙吗?

Here's the very simple code that I wrote: 这是我编写的非常简单的代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int n, m[100];
    ifstream f1("in.txt");
    f1 >> n;
    for (int i; i < n; i++)
    {
        f1 >> m[i];
        cout << m[i] << endl;
    }
    f1.close();
    system("pause");
    return 0;
}

This is the in.txt path C:\\Users\\Administratar\\source\\repos\\ConsApp1\\in.txt 这是in.txt路径C:\\ Users \\ Administratar \\ source \\ repos \\ ConsApp1 \\ in.txt

And this is what is inside in.txt 这就是in.txt中的内容

4
1
2
3
4

And the error says: "Error: Unable to open file C:\\Users\\Administratar\\source\\repos\\ConsApp1\\Debug\\ConsApp1.obj Error code = 0x80070002" 并且错误显示:“错误:无法打开文件C:\\ Users \\ Administratar \\ source \\ repos \\ ConsApp1 \\ Debug \\ ConsApp1.obj错误代码= 0x80070002”

I had a similar issue just now, actually, but the problem wasn't in ifstream or the include at all. 实际上,我刚才有一个类似的问题,但是问题根本不在ifstream或include中。 It was in the file included previous of it. 它在之前的文件中。 Just check stdafx.h file and make sure there's no issues or errors in it and it shall work perfectly fine. 只需检查stdafx.h文件,并确保其中没有任何问题或错误,即可正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM