I am trying to build a small CLI software that requests data from the NASA API APOD.
Everything works fine until I want to open the external text file that I wrote into with the default textedit (I am on a Mac) on the computer.
Here is the code for the function that handles that part:
void SaveJSONIntoTextFile(std::string &date, std::string &content) {
std::string nameOfFile;
if (date.empty()) {
nameOfFile = "apod" + currentDate();
} else {
nameOfFile = "apod" + date;
}
std::ofstream file_out;
file_out.open(nameOfFile + ".txt", std::ios::out);
file_out << content;
file_out.close();
}
I've read the C++ docs and multiple forums. The problem is that I do not want to open the file just to write in it, I want to be able to open the file with the default textedit.
For example, when the user types in a terminal: ./apod --date 2023-05-16
, I want my code to do the request, received the JSON from the API, write the JSON into a text file with the given name (all the previous points work just fine), and then open the text file with textedit or any other app that displays a text file.
When I try a simple:
std::cout << file_out << std::endl;
It doesn't work either, because as I saw in my class, I think it's a problem of operator overloading, which I do not understand at all :/
I've also tried the answers from this post: How to open a text file