首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

fgets() segmentation fault 断异常有关问题

2012-02-16 
fgets() segmentation fault 断错误问题代码:#include cv.h#include highgui.h#include sys/time.h#

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,求指正。

[解决办法]
一般是在处理char *时候出错
[解决办法]
请判断每次fgets的返回值!
[解决办法]
do while(0); goto
[解决办法]

探讨

引用:

引用:

引用:

引用:

引用:

引用:

我明白了。linux内核对于一个文件被打开的次数是有上限规定的,不能无限制的打开。

但是如果不无限……

热点排行