简体   繁体   中英

Can't write raw data on SD card under Linux in C

I am trying to write raw data on my SD card under Linux Ubuntu 14.04.

I need this cause I want to write my own "filesystem" from which I can easily read under microcontroller (arm based).

I am able to read data from SD card, but when I try to write some it gives me an error of:

" Operation not permitted ".

This is the code I'm using:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>      /* open */
#include <unistd.h>     /* exit */
#include <sys/ioctl.h>      /* ioctl */
#include <errno.h>
#define DEVICE "/dev/sdb1" /* SD card */

int main()
{
   int file_desc, ret_val;
   char msg[512];
   msg[0] = 'H';
   char answer[512];
   file_desc = open(DEVICE, O_RDWR);
   if(file_desc < 0)
      printf("failed to open the device \n");

   ret_val = write(file_desc, msg, 512);
   fsync(file_desc);
   printf("%d\n", ret_val);
   printf("%d\n", errno);
   read(file_desc, answer, 512);

   printf("message read is %s\n", answer);
   close(file_desc);
   return 0;
}

As I said, I can open file in read/write mode and read data from it, but in these lines:

ret_val = write(file_desc, msg, 512);
fsync(file_desc);
printf("%d\n", ret_val);
printf("%d\n", errno);

I get ret_val = -1 (which means error), and errno = 1 (which means " Operation not permitted").

I would be very grateful for any advice that would help me write raw data on my SD card.

在继续操作之前,请检查卡/适配器的侧面是否有写保护开关,并确保其已关闭(如果有)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM