0

I've just installed Visual Studio to start writing C++ there, I spent the last year learning in CodeBlocks and everything was going fine. 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. 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

And this is what is inside 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"

15

1 Answer 1

0

I had a similar issue just now, actually, but the problem wasn't in ifstream or the include at all. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.