read中的count问题

发布于 2022-09-23 14:25:47 字数 12933 浏览 16 评论 0

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

桃酥萝莉 2022-09-30 14:25:48

完全是你的代码写得有问题,细心点啊,大哥:

#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]);
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;
}

黑凤梨 2022-09-30 14:25:48

我不是说读和写的问题,而是说当读时,设备驱动程序的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 编辑 ]

蓦然回首 2022-09-30 14:25:47
#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("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;
}

杀お生予夺 2022-09-30 14:25:47

刚才写一个进程调度的体会去了,现在看到你的回复,先谢了!

我明白你的意思了.就算我输入时不带空格输入123456789,读6个字符的时候,驱动程序read函数中 count应该等于6才对吧,我没有用C库调用,直接采用系统调用的方式,这是你以前说的,应该不会错.可是结果依然不理解?????????

紫南 2022-09-30 14:25:47

无人回答吗?
不会这么个简单问题都回答不出来吧?
各位。。。。。。。。。。。。。。。????????????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文