linux fgets阻塞,linux下fgets(.stdin)不阻塞?解决方案

当前位置:我的异常网» Linux/Unix » linux下fgets(.stdin)不阻塞?解决方案

linux下fgets(.stdin)不阻塞?解决方案

www.myexceptions.net  网友分享于:2013-02-27  浏览:270次

linux下fgets(..,..,stdin)不阻塞??

我编写了一个修改文档的程序,但是不知道为什么用fgets(buf,1024,stdin)的时候就不阻塞,我是分为两个函数写的,两个函数分别工作的时候就正常,两个函数合起来工作的时候就会这样,求救!!

函数如下:

find函数寻找对应项,modify函数修改对应项。

#include  

extern   int   modify(FILE   *fp);

int   main()

{

FILE   *fp;

if(fp=fopen( "./t.txt ", "r+ ")==NULL)

{

printf( "there   is   no   such   file ");

return   -1;

}

find(fp);

modify(fp);

fclose(fp);

return   0;

}

int   find(FILE   *fp)

{

char   *t;

char   *idata;

char   *namebuf,*buf2,*addr,*tempbuf;

char   *searchname;

char   *tsearchname,*tnamebuf;

long   count;

long   offsize;

offsize=0;

fseek(fp,0,SEEK_SET);

searchname=(unsigned   char   *)malloc(1024);

idata=(unsigned   char   *)malloc(1024);

tsearchname=(unsigned   char   *)malloc(1024);

tsearchname=searchname;

while((*searchname=(unsigned   char)fgetc(stdin))!= ': ')

{

searchname++;

}

namebuf=(unsigned   char   *)malloc(1024);

tempbuf=(unsigned   char   *)malloc(1024);

tnamebuf=(unsigned   char   *)malloc(1024);

namebuf=tnamebuf;

while((t=fgets(tempbuf,1024,fp))!=NULL)

{

fseek(fp,offsize,SEEK_SET);

while((*tnamebuf=(unsigned   char)fgetc(fp))!= ': ')

{

tnamebuf++;

printf( "the   number   is:%s\n ",namebuf);

}

printf( "namebuf:%s\nsearchname:%s\n ",namebuf,tsearchname);

int   a=0;

if((a=strcmp(namebuf,tsearchname))==0)

break;

printf( "strcmp   is:%d\n ",a);

tnamebuf=namebuf;

fgets(tempbuf,1024,fp);

offsize=ftell(fp);

printf( "%s\n ",tempbuf);

}

if(t==NULL)

{

printf( "can 't   find   the   number\n ");

return   1;

}

else

{

printf( "the   number   %s   is   found\n ",namebuf);

printf( "end   here\n ");

}

printf( "enter   your   data   **:**\n ");

printf( "yeye\n ");

return   0;

}

int   modify(FILE   *fp)

{

char   *pointbuftemp;

char   *buf,*buf2,*buftemp;

int   rest;

long   current,next,nextnext,departure;

buf=(unsigned   char   *)malloc(1024);

buftemp=(unsigned   char   *)malloc(1024);

buf2=buf;

current=ftell(fp);

rest=1024;

fgets(buf,rest,fp);

next=ftell(fp);

departure=0;

while(fgets(buf,rest,fp)!=NULL)

{

nextnext=ftell(fp);

departure=nextnext-next;

next=ftell(fp);

rest-=departure;

buf+=departure;

}

fseek(fp,current,SEEK_SET);

fgets(buftemp,1024,stdin);

fputs(buftemp,fp);

fputs(buf2,fp);

printf( "fflush?\n ");

return   0;

}

请问这是什么原因,应该怎么办呢?谢谢!!

------解决方案--------------------

设这样的

*searchname=(unsigned char)fgetc(stdin)!= ': '

读完以后,缓冲区里的\n号没读取出来.在modify内再用fgets(buftmp,1024,stdin)读时就会把这个 '\n '读走.

这样改一下就可以了:

while((*searchname=(unsigned char)fgetc(stdin))!= ': ')

{

searchname++;

}

fgetc(stdin); //添加一句.

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值