fgets阻塞 stdin 退出_linux fgets 阻塞

在Linux环境下,遇到fgets从stdin读取时出现不阻塞的情况。问题出现在find函数中,由于未处理换行符 ,导致modify函数使用fgets时立即读取到换行符并结束。解决方案是在找到冒号后,增加一行fgetc(stdin)来读取换行符。
摘要由CSDN通过智能技术生成

当前位置:我的异常网» 热门搜索 » linux fgets 阻塞

linux fgets 阻塞

www.myexceptions.net  网友分享于:2013-09-23   搜索量:159次

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

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); //添加一句.

不过说实话,楼主的程序有很多地方都是冗余的.

你的malloc分配的空间,好象都没free吧.

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值