fgets() segmentation fault 断错误问题
代码:
#include "cv.h"
#include "highgui.h"
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
char *getstimeval(char *buf)
{
struct timeval tv;
struct tm tm;
size_t len = 28;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &tm);
strftime(buf, len, "%Y-%m-%d %H:%M:%S", &tm);
len = strlen(buf);
sprintf(buf + len, ".%6.6d", (int)(tv.tv_usec));
return buf;
}
int main(int argc,char *argv[])
{
char x[11],y[11];
char image_name[30];
FILE *fd;
fd=fopen("hello.txt","r");
if(NULL==fd)
{
printf("read file error!");
return 0;
}
printf("read file success!\n");
while((fgets(x,11,fd))!=NULL)
{
printf("%s",x);
}
fseek(fd,0L,0);
while((fgets(y,11,fd))!=NULL)
{
printf("%s",y);
}
fseek(fd,0L,0);
printf("%s",x);
printf("%s",y);
IplImage* frame;
CvCapture* capture;
capture=cvCreateCameraCapture(0);
while(1)
{
frame=cvQueryFrame(capture);
if(strcmp(x,y)!=0)
{
char buf[28];
struct timeval tv1;
gettimeofday(&tv1, NULL);
sprintf(image_name,"%s%s",getstimeval(buf),".jpg");
cvSaveImage(image_name,frame);
printf("change\n");
}
if(!frame) break;
fgets(x,11,fd);
printf("%s",x);
fseek(fd,0L,0);
char c=cvWaitKey(100);
fd=fopen("hello.txt","r");
if (c==27) break;
fgets(y,11,fd);
printf("%s",y);
fseek(fd,0L,0);
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
fclose(fd);
}
编译通过,程序要不停的运行,运行一段时间之后出现段错误。gdb了一下是fets()的问题。但是楼主不是很明白gdb,不会用。编译环境ubuntu 10.04,求指正。