Write a File in a Specific Path in C++

Write a file in a specific path in C++

Specify the full path in the constructor of the stream, this can be an absolute path or a relative path. (relative to where the program is run from)

The streams destructor closes the file for you at the end of the function where the object was created(since ofstream is a class).

Explicit closes are a good practice when you want to reuse the same file descriptor for another file. If this is not needed, you can let the destructor do it's job.

#include <fstream>
#include <string>

int main()
{
const char *path="/home/user/file.txt";
std::ofstream file(path); //open in constructor
std::string data("data to write to file");
file << data;
}//file destructor

Note you can use std::string in the file constructor in C++11 and is preferred to a const char* in most cases.

How to create file inside a directory using C

use sprintf() or similar to create the pathFilename string:

char pathFile[MAX_PATHNAME_LEN];
sprintf(pathFile, "%s\\my_log.txt", directory );

then

int filedescriptor = open(pathFile, O_RDWR | O_APPEND | O_CREAT);   

Note: If you are using linux, change \\ to / and MAX_PATHNAME_LEN to 260 (or whatever linux likes to use for that value.)

EDIT if you need to check that a directory exists before creating a file there, you can do something like this:

if (stat("/dir1/my_dir", &st) == -1) {
mkdir("/dir1/my_dir", 0700);
}

Read more here: stat, mkdir

How to create a file in a specific directory in c++11 ?!

Specify the complete path to the file in the open() function:

file.open("C:\\Users\\Foo\\Desktop\\bar.txt");

If you need the folder to be user-specific, you can use the $HOME and $USER linux environment variables and their windows equivalents.

EDIT: as somebody pointed out (thanks), you need to escape the backslashes on Windows, as they are used as escape characters in C. On Linux, you don't need to do that, as the path uses forward slashes.

How to write file inside folder in current directory using fopen

Assuming you meant int number as opposed to your number[] which is not valid C.

You can use sprintf(), or preferably snprintf():

void record_data(int number) {
char str[255]; //Large enough buffer.
snprintf(str, sizeof(str), "./folder/%d", number);
FILE *fptr;
fptr = fopen(str, "w");
}

And consider calling fclose() on your FILE * when you're done using it.

Write to a file in the current directory

You should be able to use the following code:

FILE *fp = fopen("output.txt", "w+");

The file "output.txt" would be in the current directory.

Writing the correct path in fprintf function

Use perror() to have the Operating System help you determine the cause of failure.

#define FILENAME "C:/Users/User1/Desktop/myfile.txt"

fp = fopen(FILENAME, "w+");

// report and shut down on error
if (fp == NULL) {
perror(FILENAME);
exit(EXIT_FAILURE);
}

How do I create a directory in a given path in C++?

I would suggest using the <filesystem> library which is available as of C++17, in this case using std::filesystem::create_directories

#include <filesystem>

void create()
{
std::filesystem::path subfolder = "/folder1/name1";
std::filesystem::create_directories(subfolder);
}

int main()
{
create();
}

Note that create_directory assumes all parent directories exist, otherwise create_directories will create intermediate directories if missing.



Related Topics



Leave a reply



Submit