#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> int main(int argc,char** argv) { char *buf=malloc(atoi(argv[2])); int fn,n,lk; //printf("sizeof(buf)=%i\n",sizeof(buf));
memset(buf,0,atoi(argv[2])); if(argc!=4){ printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n"); exit(1); } lk=atoi(argv[3]); if((fn=open(argv[1],O_RDWR))==-1){ printf("openfilenew:open file %sfailure because %s\n",argv[1],strerror(errno)); exit(1); }switch(lk){ case 1: { int nm=0,k,count=atoi(argv[2]); while(nm<count){ k=read(fn,buf,count-nm); if(k==-1){ printf("error:%s",strerror(errno));exit(1);} nm=nm+k; }; /*if(n==-1){ printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno)); free(buf); close(fn); exit(1); } if(n<atoi(argv[2])) printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2])); */ buf[nm]=0; printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]); close(fn); free(buf); exit(0); } case 2: { printf("please input %i data of char for writing\n",atoi(argv[2])-1); int kk=0; //gets(buf);
fgets((char*)buf,(int)atoi(argv[2]),stdin); printf("the act input %i bytes from stdin\n",atoi(argv[2])-1); n=write(fn,buf,atoi(argv[2])); if(n==-1) printf("openfilenew:write to %s failure\n",argv[1]); if(n<atoi(argv[2])) printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf)); close(fn); exit(0); break; } default: printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n"); exit(1); } free(buf); return 0; }
|
发布评论
评论(5)
完全是你的代码写得有问题,细心点啊,大哥:
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));
memset(buf,0,atoi(argv[2]));
if(argc!=4){
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
printf("openfilenew:open file %sfailure because %s\n",argv[1],strerror(errno));
exit(1);
}
switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
k=read(fn,buf,count-nm);
if(k==-1){
printf("error:%s",strerror(errno));exit(1);}
nm=nm+k;
};
/*if(n==-1){
printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
free(buf);
close(fn);
exit(1);
}
if(n<atoi(argv[2]))
printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/
buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
int read_cnt;
for(read_cnt = 0; read_cnt < nm; read_cnt++)
printf("%c\n", buf[read_cnt]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;
//gets(buf);
fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
int cnt = 0;
for (;cnt < atoi(argv[2]); cnt++)
printf("%c\n", buf[cnt]);
exit(0);
break;
}
default:
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
free(buf);
return 0;
}
我不是说读和写的问题,而是说当读时,设备驱动程序的file相应read函数里边count反应出来的问题:
ssize_t do_short_read (struct inode *inode, struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "short: count of do_short_read function = %i\n",count);
....
}
针对测试程序:
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
k=read(fn,buf,count-nm);
if(k==-1){
printf("error:%s",strerror(errno));exit(1);}
nm=nm+k;
};
这里面的read的count和驱动程序里面do_short_read的count不一致.
[ 本帖最后由 whoisliang 于 2009-1-20 17:08 编辑 ]
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));
memset(buf,0,atoi(argv[2]));
if(argc!=4){
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
printf("openfilenewpen file %sfailure because %s\n",argv[1],strerror(errno));
exit(1);
}
switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
k=read(fn,buf,count-nm);
if(k==-1){
printf("error:%s",strerror(errno));exit(1);}
nm=nm+k;
};
/*if(n==-1){
printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
free(buf);
close(fn);
exit(1);
}
if(n<atoi(argv[2]))
printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/
buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;
//gets(buf);
fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
int cnt = 0;
for (;cnt < atoi(argv[2]); cnt++)
printf("%d\n", buf[cnt]);
exit(0);
break;
}
default:
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
free(buf);
return 0;
}
刚才写一个进程调度的体会去了,现在看到你的回复,先谢了!
我明白你的意思了.就算我输入时不带空格输入123456789,读6个字符的时候,驱动程序read函数中 count应该等于6才对吧,我没有用C库调用,直接采用系统调用的方式,这是你以前说的,应该不会错.可是结果依然不理解?????????
无人回答吗?
不会这么个简单问题都回答不出来吧?
各位。。。。。。。。。。。。。。。????????????