The fstream open() does not create a file

I am trying to write in a file using this code in visual studio 2017:

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

using namespace std;

int main()
{
ofstream output("newfile");

//output.open("C:/Users/Muhammad/Desktop/scores.txt");
if (!output)
cout << "error";

output << "any word" << endl;

output.close();

cin.get();

}

I have made the location of the project directory in the desktop but still the file is not created nor opened. when I compile the code it gives success but console window display "error".
Last edited on
aah, yes I had this problem too.

first off you have to specify what kind of file you want(txt, bi, exe)

like this

1
2
3
4
5
ofstream output;
	output.open("file.txt");
	output << "write this cuz i told you to";
	output.close();
//this makes, opens, writes, and then closes a txt file 

once that is done open file explorer,
find Windows (C:)
open the file that says users
then open the file that has your account name on it
in that should be a file that says source
in that should be one that says repos
in that will be your VS source files. select the one that you created the file in, and it should be in there.

again that is:

file explorer

Windows (C:)

users

file with account name

source

repos

find source file you made the file in

find the file.


I have tried to do this, but the folder "repos" is empty.
When you save your projects, do u specify that you want them saved in repos.


if not just find the file that holds the projects, it should be contained in there.
I saved the project on the desktop, and the code compiles but still no file is created in the project directory.
specify the exact folder for a quick test..
output.open("c:/someplace/you/desktop/project/etc/file.txt"); //you need to fill in the path yourself for your exact machine.

if that does not work, your program may lack permission to write to your desktop; you may need to make a dump folder in c:\ and write there, or move the project (this can explode if you had hard paths in it anywhere) somewhere that it does have permissions.

there remains an option that it wrote it somewhere unexpected.
you can do dir/s filename to search for it quickly
Last edited on
errno is what you use for rich errors that C++ stream errorbits can't help with. I find it quite useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <fstream>
#include <cerrno>
#include <cstring>

//file handle is a dummy wrapper that combines a vector and a filename together (for reference if an error happens)
FileHandle::FileHandle(const std::string& file)
{
  file_name = file;

  std::ifstream in(file_name, std::ios::binary);

  if(!in)
  {
    logger<< file_name <<" error: "<<std::strerror(errno)<<std::endl;
    return;
  } else {
    //get info
    in.seekg (0, in.end);
    int length = in.tellg();
    in.seekg (0, in.beg);

    if(length == 0) {
      logger<< file_name <<" error: the file is empty"<<std::endl;
      return;
    }

    //put data in
    file_data.resize(length);
    in.read(file_data.data(),length);

    //not !in.good() or !in because of EOF I believe.
    if(in.fail())
    {
      logger<< file_name <<" mid read error: "<<std::strerror(errno) <<std::endl;
      return;
    }
  }
}


You can also try to get the cwd which can help, with this:
1
2
3
4
5
6
7
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current path is " << fs::current_path() << '\n';
}
Topic archived. No new replies allowed.