Can't create and write in files on MicroSD card

Hello,

I am trying to write data in an existing file and also create a file that should be located in the Micro SD card on the Ixora board.
The problem is that when I try to write in a file I get an error that the file can’t be open and when I try to create a file I get an error that a file cannot be created.
These are the functions that I wrote:

#define PATH_FILES "/media/A68C-7007"
void createFile(const string& dirPath, const string& fileName) {
    string filePath = dirPath + "/" + fileName;
    ofstream file(filePath.c_str());
    if (!file) {
        cout << "Error: could not create file " << filePath << endl;
    } else {
        cout << "File " << filePath << " created successfully." << endl;
        file.close();
    }
}

void write_to_file(const string& filePath, const string& newLine) {
    ofstream file;
    file.open(filePath.c_str(), ios::out | ios::app);
    if (file.is_open()) {
        file << newLine << endl;
        file.close();
        cout << "New line added to file." << endl;
    } else {
        cout << "Error: could not open file." << endl;
    }
}
void save_new_file(){
    string extention_str = ".txt";

    //check if this file already exists
    string filename = getCurrentDate() + extention_str;
    int fileExists = fileExistsInDir(PATH_FILES, filename);
    if (fileExists) {
        cout << "File " << filename << " exists in directory " << PATH_FILES << endl;
    } else {
        cout << "File " << filename << " does not exist in directory " << PATH_FILES << endl;
        cout << "Creating file" << PATH_FILES << endl;
        createFile(PATH_FILES, filename);
    }


    write_to_file("/media/A68C-7007/zdr.txt", "ZDR.");
    read_file("/media/A68C-7007/zdr.txt");
}


I can read the existing file with no problems.

Because of this I think that the problem might be with some of the permission configurations on the drive but I am not sure what I need to do to allow the writing and creating of files.

Greetings @Svetoslav,

What permissions does your SD card have? I assume this code is being executed as the “torizon” user. If your SD card requires root user permissions, then it would make sense that this fails. If your SD/USB driver is FAT formatted (which most are by default) then it gets auto-mounted with root user permissions.

You would need to re-mount your driver with proper permissions such that the “torizon” user can work with it. One method is detailed in this thread: R/W access permissions to torizon user/containers on auto-mounted USB drive

Best Regards,
Jeremias

Hello,

My previous configuration there was:
#/dev/mmcblk0p1 /media/card auto defaults,sync,auto,nofail,uid=1000,grid=1000 0 0

and now I changed it like the given one in the problem you sent me to:
/dev/mmcblk0p1 /media/card auto relatime,nofail,utf8,uid=torizon,gid=torizon,umask=002 0 0

But I get the same result. Still can’t create or write in a file but I can read the contents of an existing file.

Strange this works for me and my USB drive. What are the permissions/ownership of your mounted SD card directory and the files within?

As the “torizon” user can you write files to the SD card outside the container on the base OS? Or is the same behavior as trying to write files when inside the container?

Also just to confirm can you actually write to the SD card with root/sudo permissions?

Best Regards,
Jeremias

I managed to fix the problem.
I had to remount the drive for it to take effect which was a step I had forgotten.

Thank you for your time.

Glad I was able to help clear things up.