opencv使用中的分段错误

时间:2014-12-16 13:57:35

标签: segmentation-fault fopen

在Ubuntu 12.04上运行

以下程序首先从文件中读取50个字符,然后将这些值转换为int。 实际上文件中的值是这样的:

  

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

我只是打开一个文件然后关闭它,但我有一些openCV特定的头文件包含在内:

#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;
    }

但它总是会给出分段错误。

我编译此文件的方式是:

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

我有一个cmake文件( cMakeLists ),其中包含以下内容:

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})

然后使用cmake编译为

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

我得到分段错误。

以下是带核心的gdb的输出

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>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/ravi/Desktop/New/build/new1...done.
[New LWP 4105]

warning: Can't read pathname for 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 was generated by `./new1'.
Program terminated with signal 11, Segmentation fault.
#0  0xb757a2f2 in fclose () from /lib/i386-linux-gnu/libc.so.6
(gdb) backtrace
#0  0xb757a2f2 in fclose () from /lib/i386-linux-gnu/libc.so.6
#1  0x08048800 in main () at /home/ravi/Desktop/New/new1.cpp:35
(gdb) frame 1
#1  0x08048800 in main () at /home/ravi/Desktop/New/new1.cpp:35
35  fclose(fp);
(gdb)print fp 
$1= (FILE *) 0x0 

从这里我可以看到我打开文件的方式有些问题。但这里有什么问题?

解决

克里斯是对的。当我添加\ n时,它正在打印“文件打开失败”。我看到Xvalues.txt所在的目录与我的二进制文件不同。

1 个答案:

答案 0 :(得分:1)

正如聊天中所讨论的那样;有两个问题:

  1. 打印错误时没有换行符
  2. 文件指针为NULL
  3. <强>换行

    在打印某些内容时,您需要在最后添加换行符(或使用fflush)。否则它不会显示。

    printf("File opening failed");
    

    文件指针为NULL

    可能您的程序找不到您要读取的文件,或者您的权限错误。

相关问题