Search code examples
c++iofstreamiomanip

unable to open file in visual studio


im learning c++ using visual studio as ide. I'm currently doing io streams, but when i try to open a file, the program doesn't open the file.

here is the code -

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>

int main()
{
    std::ifstream file;

    file.open("Text.txt");

    if (file.is_open())
    {
        std::cout << "open" << std::endl;
    }
    else
        std::cout << "not open" << std::endl;
 
}

i get the output as not open.

any help is appreciated, thanks


Solution

  • you need to keep the file in the directory where the executable is generated by default when using Visual Studio. this is typically located in your solution directory under a folder called Debug/Release depending on your configuration. check the project settings to see where the executable will be generated and copy the file there.