fopen - Segmentation fault in opencv usage -


running on ubuntu 12.04

the following program first reads 50 characters file, , converts these values int. values in file these:

0020.50 0020.49 0020.47 0020.46 0020.51 0020.50 0020.50 0020.49 0020.49 0020.50 0020.50 0020.52 0020.51 0020.47 0020.49 0020.48 0020.48 0020.50 0020.49 0020.49 0020.48 0020.50 0020.48 0020.50 0020.51 0020.47 0020.47 0020.47 0020.49 0020.50 0020.47 0020.53 0020.42 0020.57 0020.43 0020.47 0020.49 0020.50 0020.46 0020.49 0020.40 0020.48 0020.32 0020.39 0020.55 0021.39 0020.66 0020.77 0020.34 0019.52 0052 24.81

i opening file , closing it, have opencv specific header files included:

#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> #include <math.h> #include "opencv/cv.h" #include "opencv/highgui.h"  using namespace cv; using namespace std;  int main()     {     file *fp;     int i,x;     int read;      char * buffer;   buffer =(char *)malloc(100);       fp=fopen("xvalues.txt","r");      if(fp==null)         {         printf("file opening failed");         }  printf("file opened successfully");        fclose(fp);     return 0;     } 

but gives segmentation fault.

the way compiling file is:

mkdir build && cd build && cmake .. && make 

and have cmake file (cmakelists)with following content:

cmake_minimum_required(version 2.4.9)  project(demo)  find_package(opencv required) include_directories(${opencv_include_dirs})   add_definition("-g2")  add_executable(new1 new1.cpp) target_link_libraries(new1 ${opencv_libs}) 

and compile using cmake

mkdir build && cd build && cmake .. && make 

i segmentation fault.

here output of gdb core

root@ubuntu:/home/ravi/desktop/new/build# gdb new1 core gnu gdb (ubuntu/linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 copyright (c) 2012 free software foundation, inc. license gplv3+: gnu gpl version 3 or later <http://gnu.org/licenses/gpl.html> free software: free change , redistribute it. there no warranty, extent permitted law.  type "show copying" , "show warranty" details. gdb configured "i686-linux-gnu". bug reporting instructions, please see: <http://bugs.launchpad.net/gdb-linaro/>... reading symbols /home/ravi/desktop/new/build/new1...done. [new lwp 4105]  warning: can't read pathname load map: input/output error. [thread debugging using libthread_db enabled] using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1". core generated `./new1'. program terminated signal 11, segmentation fault. #0  0xb757a2f2 in fclose () /lib/i386-linux-gnu/libc.so.6 (gdb) backtrace #0  0xb757a2f2 in fclose () /lib/i386-linux-gnu/libc.so.6 #1  0x08048800 in main () @ /home/ravi/desktop/new/new1.cpp:35 (gdb) frame 1 #1  0x08048800 in main () @ /home/ravi/desktop/new/new1.cpp:35 35  fclose(fp); (gdb)print fp  $1= (file *) 0x0  

from can see thing wrong way opening file. wrong here ?

solved

chris right. when added \n, printing "file opening failed". saw directory xvalues.txt lies different binary.

as discussed in chat; there 2 problems:

  1. no newline when printing error
  2. the file pointer null

newline

when printing need put newline @ end (or use fflush). otherwise won't show.

printf("file opening failed"); 

file pointer null

probably program not find file want read or have wrong permissions.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -