当前位置: 技术问答>linux和unix
linux下fgets(..,..,stdin)不阻塞??
来源: 互联网 发布时间:2016-02-03
本文导语: 我编写了一个修改文档的程序,但是不知道为什么用fgets(buf,1024,stdin)的时候就不阻塞,我是分为两个函数写的,两个函数分别工作的时候就正常,两个函数合起来工作的时候就会这样,求救!! 函数如下: find函数寻...
我编写了一个修改文档的程序,但是不知道为什么用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:%sn",namebuf);
}
printf("namebuf:%snsearchname:%sn",namebuf,tsearchname);
int a=0;
if((a=strcmp(namebuf,tsearchname))==0)
break;
printf("strcmp is:%dn",a);
tnamebuf=namebuf;
fgets(tempbuf,1024,fp);
offsize=ftell(fp);
printf("%sn",tempbuf);
}
if(t==NULL)
{
printf("can't find the numbern");
return 1;
}
else
{
printf("the number %s is foundn",namebuf);
printf("end heren");
}
printf("enter your data **:**n");
printf("yeyen");
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;
}
请问这是什么原因,应该怎么办呢?谢谢!!
函数如下:
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:%sn",namebuf);
}
printf("namebuf:%snsearchname:%sn",namebuf,tsearchname);
int a=0;
if((a=strcmp(namebuf,tsearchname))==0)
break;
printf("strcmp is:%dn",a);
tnamebuf=namebuf;
fgets(tempbuf,1024,fp);
offsize=ftell(fp);
printf("%sn",tempbuf);
}
if(t==NULL)
{
printf("can't find the numbern");
return 1;
}
else
{
printf("the number %s is foundn",namebuf);
printf("end heren");
}
printf("enter your data **:**n");
printf("yeyen");
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吧.
*searchname=(unsigned char)fgetc(stdin)!=':'
读完以后,缓冲区里的n号没读取出来.在modify内再用fgets(buftmp,1024,stdin)读时就会把这个'n'读走.
这样改一下就可以了:
while((*searchname=(unsigned char)fgetc(stdin))!=':')
{
searchname++;
}
fgetc(stdin); //添加一句.
不过说实话,楼主的程序有很多地方都是冗余的.
你的malloc分配的空间,好象都没free吧.