SD card access

SD card access

  • Hello!

    I'm trying to write to the SD card.

    - My board boots from NFS.

    - SD card is successfully detected when inserted.

    - I can read the contents from the shell (picocom) using ls. I can also cat the files and see their contents.

    - I have verified that the access permissions are right (just in case, I made it 777)

    but I can't open a file on the SD card.

    Here is the function. I used

    int FileTest(void) {
        int i;
        fp = fopen("/mnt/sd/test.txt", "rw");
        if(fp == NULL) {
            printf("Error in opening file\n");
            return -1;
        }
        fprintf(fp, "This is  a test.\n");
        fclose(fp);
        return 0;
    }

    But I also verified that if I change the file location to NFS, it works perfectly fine,

    no byte is missing.

    Any hint about how to write to the SD card?

    Thanks,

    Pascal

     

  • What is the output of

    mount

    I suspect you have SD card mounted read only and you are trying to open the file  read/write.

    Todd

  • Hello Todd!

    Thanks for your reply.

    The output of mount ls:

    /dev/mmcblk0p1 on /mnt/sd type vfat (rw,fmask=0022,dmask=0022,codepage=cp437,iocharset=iso8859-1)

    so it seems it's mounted read + write. I'm not sure of the masks.

    I mounted manually with the command

    mount -t vfat /dev/mmcblk0p1 /mnt/sd

    I even tried to add -o rw, but as it is the default, it doesn't change anything.

    Some other results:

    % touch /mnt/sd/hello.txt makes an empty file just as it's supposed to.

    %cp /examples/hello.txt /mnt/sd also works fine and the file contents are right.

    Pascal

  • Hello!

    Some update info:

    After playing with files for a while, It happened to work randomly. In fact, it's not

    random at all. Apparently fopen cannot open a non-existing file even for writing.

    One work around, I have added a system("touch /mnt/sd/myfile.txt"); and then I can open

    it and write some data.

    Does anyone know a trick to solve this?

    Thanks,

    Pascal

  • >  fp = fopen("/mnt/sd/test.txt", "rw");

    from "man fopen"

       w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at

                 the beginning of the file.

    try:

    fp = fopen("/mnt/sd/test.txt", "w+");

  • Thanks Todd!

    And sorry about that, I should read docs more often!

    Pascal