unable to open file in visual studio

Viewed 1434

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

3 Answers

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.

I had a similar problem opening the csv file from cpp file and I found a solution by placing my csv file in the same folder where my source files and especially the cpp file(from which I am trying to open it) is placed.

1)Make sure the path you given is correct.

2)Make sure the file you trying to open is already using by other processes.

  1. Make sure the file exists the path given and and try by giving the full path of the file , if it works ,then make your adjustments.

  2. Try to open the file manually and check if their is any problem with the file.

  3. Check your file is protected by read-only or something like that.

  4. Try to work with the below sample code, and reply me what error you are getting.

file reading sample

file writing sample

Related