Search code examples
c++stringvisual-studio-codefstreamifstream

fstream doesn't read the input Visual Studio Code but it works in Visual Studio Community


The code below works properly in Visual Studio Community 2019, the input file opens and gets read.

When I try the same code in Visual Studio Code, it doesn't work, and returns "access denied".

I need to use Visual Studio Code.

The input file is in the .exe's directory in case of Visual Studio Code, and in the .cpp's directory in case of VS Community.

VS Community Screenshot: image

VS Code Screenshot: image

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    fstream file;
    string word;
    file.open("input.txt");
    getline(file, word);

    if (file.is_open() == true)
        cout << "access aproval" << endl;
    else
        cout << "access denied" << endl;

    cout << word << endl;
}

Solution

  • Try to give the full location of the file. file.open("C\..\input.txt");