Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C linux编程:写入设备文件_C_Linux - Fatal编程技术网

C linux编程:写入设备文件

C linux编程:写入设备文件,c,linux,C,Linux,我写道: #include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <mtd/mtd-user.h> #include <errno.h> int main( void ) { int fd; char buf[4]="abc"; fd = open("/dev/mtd0", O_RDWR);

我写道:

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>
#include <errno.h>

int main( void )
{
        int fd;
        char buf[4]="abc";

        fd = open("/dev/mtd0", O_RDWR);
        lseek(fd, 1, SEEK_SET);
        write(fd, &buf, 4);
        close(fd);
        perror("perror output:");

        return 0;
}
获得有意义的输出。运行程序后,它的输出:

perror output:: Invalid argument

如果我的程序中有任何错误?

您应该有这样的程序

if(-1 == write(fd, &buf, 4)){
  perror("perror output:");
}
close(fd);
因为perror显示了最后一个错误

关于perror的更多信息,也许这有帮助

这一切都必须涉及访问权


正如Jakub和Mat所说,检查每个API调用的错误代码。

是的,存在问题。您使用的
peror()
是错误的

在调用perror之前,您应该首先检查系统调用是否表明存在问题。 手册页对该主题非常明确:

Note that errno is undefined after a successful library call: this call
may  well  change  this  variable, even though it succeeds, for example
because it internally used some other  library  function  that  failed.
Thus,  if  a failing call is not immediately followed by a call to per‐
ror(), the value of errno should be saved.
您应该检查每个系统的返回码,只有当它们失败时才调用perror。 大概是这样的:

fd = open("/dev/mtd0", O_RDWR);
if (fd < 0) {
    perror("open: ");
    return 1;
}
if (lseek(fd, 1, SEEK_SET) < 0) {
    perror("lseek: ");
    return 1;
}
if (write(fd, &buf, 4) < 0) {
    perror("write: ");
    return 1;
}
close(fd);
fd=open(“/dev/mtd0”,O_RDWR);
如果(fd<0){
佩罗尔(“公开:”);
返回1;
}
if(lseek(fd,1,SEEK_SET)<0){
佩罗尔;
返回1;
}
if(写入(fd和buf,4)<0){
佩罗尔(“写:”);
返回1;
}
关闭(fd);

故障出在这一行:

if (write(fd, &buf, 4) < 0) {
if(写入(fd和buf,4)<0){
write调用的第二个参数必须是指针,“buf”已经是指针,用“&”引用它,您将获得指向错误指针的指针:正确的调用是:

if (write(fd, (void*)buf, 4) < 0) {
if(写入(fd,(void*)buf,4)<0){

您可能需要编写整个页面,而不仅仅是4个字节

您可以通过在shell中键入命令
dmesg
来确认这一点。 然后您将看到以下内核消息:

nand_do_write_ops:尝试写入非页面对齐的数据

然后将要写入mtd的代码替换为:

char buf[2048]="abcdefghij";                      //Ajust size according to 
                                                  //mtd_info.writesize
mtd_info_t mtd_info;                              // the MTD structure

if (ioctl(fd, MEMGETINFO, &mtd_info) != 0) {...   // get the device info

memset(buf+10, 0xff, mtd_info.writesize - 10);    //Complete buf with 0xff's

if (write(fd, &buf, mtd_info.writesize) < 0) {... // write page
char buf[2048]=“abcdefghij”//根据
//mtd_info.writesize
mtd_info\u t mtd_info;//mtd结构
如果(ioctl(fd、MEMGETINFO和mtd_info)!=0){…//获取设备信息
memset(buf+10,0xff,mtd_info.writesize-10);//用0xff完成buf
if(write(fd,&buf,mtd_info.writesize)<0){…//写入页面

还考虑检查坏块(<代码> iOCTL(FD,MMGED BADBULD,…<代码>)和擦除块(<代码> iOCTL(FD,MeMease..…<代码>))。


希望这能有所帮助。

您的错误报告是错误的。您需要检查每个单独的系统调用/库函数的返回值,并在调用失败后立即使用
peror
,而不需要干预函数调用。如前所述,您的
peror
调用根本没有提供任何信息。在C中,对于在e stack、
&buf
&buf[0]
以相同的地址结束,因此它不是错误的来源(尽管它仍然是需要纠正的)。
char buf[2048]="abcdefghij";                      //Ajust size according to 
                                                  //mtd_info.writesize
mtd_info_t mtd_info;                              // the MTD structure

if (ioctl(fd, MEMGETINFO, &mtd_info) != 0) {...   // get the device info

memset(buf+10, 0xff, mtd_info.writesize - 10);    //Complete buf with 0xff's

if (write(fd, &buf, mtd_info.writesize) < 0) {... // write page