-2

I have some trouble with producing files in C++. I consulted this answer here but when I try using it, it doesn't produce a file. What I wrote:

//~/Documents/Test_CPP/ex2/main_2.cpp

#include <fstream>

int main()
{
    std::ofstream file("Hello.txt");
    // Hello.txt has been created here
}

I compile it with the command g++ main_2.cpp and run it with ./a.out. I don't really know what could go wrong here, except theorizing that the file might be produced not in the current directory but somewhere else. So I tried changing Hello.txt to ~/Documents/Test_CPP/ex2/Hello.txt, which doesn't change anything. What exactly am I doing wrong here?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Sito
  • 494
  • 10
  • 29
  • 4
    "the file is not produced in the current directory but somewhere else" - where? – Jesper Juhl Jan 11 '20 at 17:32
  • @JesperJuhl I thought it might be produced somewhere else, but I didn't know. So I tried giving it the full Path.. I'm sorry, but I don't think I understand your question correctly.. – Sito Jan 11 '20 at 17:34
  • 1
    My question is very simple. You say the file is not created in the current working directory. So all I am asking is, where *is* the file created? – Jesper Juhl Jan 11 '20 at 17:37
  • @JesperJuhl Ah, I'm sorry. It's not created at all.. At least I can't find it.. – Sito Jan 11 '20 at 17:38
  • After you have answered Jesper's question, take a look at your second statement `~/Documnets/.../main_2.cpp` what are you doing there compared to `Hello.txt`? – picklepick Jan 11 '20 at 17:39
  • Be careful with setting an existing file as an output file, because that file may then be overwritten! – Andreas Wenzel Jan 11 '20 at 17:48
  • @rezi thank you for pointing that out.. I misspelled when writing the question, I did write `file(~/... /Hello. txt) ` – Sito Jan 11 '20 at 17:52
  • @JesperJuhl I just tried searching for the file with `find / -name Hello.txt` and it really isn't anywhere on the machine. – Sito Jan 11 '20 at 18:27
  • The code works for me. If the file doesn't exist, it's created, and if it does exist, it's emptied. Are you in a directory where you have permission to create files? (Try something like `echo "File Contents" > Hello.txt` from the Linux command line? Add error checking within your code to see if `file` is open?) – JaMiT Jan 11 '20 at 19:56

3 Answers3

0

I have encountered this problem on macOS with Xcode if you use some IDEs you should point to build-dir.

desperat0
  • 56
  • 6
0

My suggestion: use std::filesystem::current_path(). It will give full path to you elf\exe dir.

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


int main() {

    std::string file_name{"Hello.txt"};

    auto path{std::filesystem::current_path()};
    path = path / file_name;
    if (std::filesystem::exists(path)) {
        std::filesystem::remove(path);
    } 

    std::ofstream out_stream(path, std::ios::out);
    if (!out_stream.is_open()) {
        std::cerr << "Error open file" << std::endl;
        return -1;
    }

    out_stream << "test" << std::endl;

    out_stream.close();

    return 0;
}
Oh-Ben-Ben
  • 163
  • 2
  • 7
-3

This can sometimes happen if you do not properly terminate the connection to the file

EG. file.close();

This must be done before the program terminates.

d0rf47
  • 409
  • 8
  • 20