我正在尝试在 Linux Ubuntu 14.04 下将原始数据写入我的 SD 卡。
我需要这个因为我想编写我自己的“文件系统”,我可以在微 Controller (基于 arm)下轻松地从中读取。
我能够从 SD 卡读取数据,但是当我尝试写入一些数据时,出现以下错误:
“不允许操作
”。
这是我正在使用的代码:
#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;
}
正如我所说,我可以在 read/write
模式下打开文件并从中读取数据,但在这些行中:
ret_val = write(file_desc, msg, 512);
fsync(file_desc);
printf("%d\n", ret_val);
printf("%d\n", errno);
我得到 ret_val = -1
(表示错误)和 errno = 1
(表示“不允许操作”)。
如果有任何建议可以帮助我在 SD 卡上写入原始数据,我将不胜感激。
最佳答案
在继续之前,请检查卡/适配器侧面是否有写保护开关并确保它已关闭(如果存在)。
关于无法在Linux下用C在SD卡上写入原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29291593/