sophiazhang
以只写方式打开设备时失败?????为什么????
以只读方式打开时可以,以只写方式打开设备就失败,为什么??是因为权限问题吗??????
/*以只读方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",0);
/*以只写的方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",1);
/*判断打开是否成功,f>=0表示成功,否则表示失败*/
if(f>=0)
{
printf("The open operation to write succeed! /n");
}
else
{
printf("The open operation to write failed! /n");
return 0;
终端显示:
The open operation to read succeed!
How many chars do you want to read:(1-80)80
The operation to read succeed!
The actual number of chars which has been read is 28
Welcome to use this device.
The open operation to write failed!
如何解决这一问题呢?
anhongkui
/*以只读方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",0);
/*以只写的方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",1);
/*判断打开是否成功,f>=0表示成功,否则表示失败*/
1. 你到底要只读还是只写?
2. open的flag最好不是这么用的。
3. 学习一下C语言。
sophiazhang
/*以只读方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",0);
/*判断打开是否成功,f>=0表示成功,否则表示失败*/
if(f>=0)
{
printf("The open operation to read succeed! /n");
}
else
{
printf("The open operation to read failed! /n");
return 0;
}
printf("How many chars do you want to read:(1-80)");
/*输入要读出的字符的个数*/
scanf("%d",&num);
scanf("%c",&buffer[0]); /*吸收之前输入的换行符*/
/*判断要读出的字符的个数是否合法,一定要在1-80之间*/
while(num>80 || num<1)
{
printf("The number you want to read is invalid! It must be between 1 and 80!/n");
scanf("%d",&num);
scanf("%c",&buffer[0]); /*吸收之前输入的换行符*/
}
/*对设备进行读操作*/
i=read(f,buffer,num);
/*判断读操作是否成功,i>=0表示成功,否则表示失败*/
if(i>=0)
{
printf("The operation to read succeed! /n");
}
else
{
printf("The operation to read failed! /n");
return 0;
}
/*输出真正读出的字符的个数*/
printf("The actual number of chars which has been read is %d/n",i);
/*显示从设备里读出的的字符*/
j=0;
while (j<i){
printf("%c",buffer[j]);
j++;}
printf("/n");
/*关闭文件*/
close(f);
/*对设备进行写操作*/
f=-1;
/*以只写的方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",1);
/*判断打开是否成功,f>=0表示成功,否则表示失败*/
if(f>=0)
{
printf("The open operation to write succeed! /n");
}
else
{
printf("The open operation to write failed! /n");
return 0;
}
printf("/n How many chars do you want to write(1-80):");
/*输入要写入的字符的个数*/
scanf("%d",&num);
scanf("%c",&buffer[0]); /*吸收之前输入的换行符*/
/*判断要写入的字符的个数是否合法,一定要在1-80之间*/
while(num>80 || num<1)
{
printf("The number is invalid! It must be between 1 and 80, please input the number you want to read again:");
scanf("%d",&num);
scanf("%c",&buffer[0]); /*吸收之前输入的换行符*/
}
printf("Please input the content which you want to write to the dev:");
/*输入要显示到设备的字符串*/
j=0;
while (j<num+1)
{
scanf("%c",&buffer[j]);
j++;
}
/*对设备进行写操作*/
i=write(f,buffer,num);
/*判断写操作是否成功,i>=0表示成功,否则表示失败*/
if(i>0)
{
printf("The write operation succeed! /n");
}
else
{
printf("The write operation failed! /n");
return 0;
}
/*输出真正写入的字符的个数*/
printf("The actual number of chars has been written into is %d/n",i);
/*关闭设备*/
close(f);
k=i;
/*检验写操作后的mychardev里的数据是什么*/
f=-1;
/*以只读方式打开设备*/
f=open("/home/sophiazhang/桌面/5/mychardev",0);
/*判断打开是否成功,f>=0表示成功,否则表示失败*/
if(f>=0)
{
printf("The open operation to read succeed! /n");
}
else
{
printf("The open operation to read failed! /n");
return 0;
}
/*对设备进行读操作*/
i=read(f,buffer,80);
if(i>=0)
{
printf("The operation to read succeed! /n");
}
else
{
printf("The operation to read failed! /n");
return 0;
}
/*输出写入设备里的内容*/
printf("The characters you just input is:");
j=0;
while (j<k){
printf("%c",buffer[j]);
j++;}
printf("/n");
/*关闭设备*/
close(f);
因为两个open函数之间有很多代码,所以我刚刚没有打出来,我的程序完成两个功能,先是只读,然后输出,再是只写,然后输出
还有,我的open格式并没有问题,因为第一个以只读方式打开时,运行成功,终端输出正确
所以我才不明白,为什么以只写方式打开时会 失败
能告诉我原因和解决方法吗 ?