c++ - 在 Ubuntu : "Invalid or incomplete multibyte or wide character", 和有趣的 UTF-8 字符上执行编译文件

标签 c++ opencv ubuntu compiler-construction exe

我正在尝试在 Ubuntu 中编译和执行一个完全正常的 .cpp 文件。 我有 Ubuntu 12.04 和 OpenCV 2.4.7。

我正在使用的文件是 mergevec.cpp:mergevec.cpp code 为了方便起见,我也把代码贴在这里:

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "cvhaartraining.h"
#include "_cvhaartraining.h" // Load CvVecFile
// Write a vec header into the vec file (located at cvsamples.cpp)
void icvWriteVecHeader( FILE* file, int count, int width, int height );
// Write a sample image into file in the vec format (located at cvsamples.cpp)
void icvWriteVecSample( FILE* file, CvArr* sample );
// Append the body of the input vec to the ouput vec
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight );
// Merge vec files
void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height );

// Append the body of the input vec to the ouput vec
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight )
{
CvMat* sample;

if( *showsamples )
{
cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
}
if( !feof( in.input ) )
{
in.last = 0;
in.vector = (short*) cvAlloc( sizeof( *in.vector ) * in.vecsize );
if ( *showsamples )
{
if ( in.vecsize != winheight * winwidth )
{
fprintf( stderr, "ERROR: -show: the size of images inside of vec files does not match with %d x %d, but %d\n", winheight, winwidth, in.vecsize );
exit(1);
}
sample = cvCreateMat( winheight, winwidth, CV_8UC1 );
}
else
{
sample = cvCreateMat( in.vecsize, 1, CV_8UC1 );
}
for( int i = 0; i < in.count; i++ )
{
icvGetHaarTraininDataFromVecCallback( sample, &in );
icvWriteVecSample ( out.input, sample );
if( *showsamples )
{
cvShowImage( "Sample", sample );
if( cvWaitKey( 0 ) == 27 )
{
*showsamples = 0;
}
}
}
cvReleaseMat( &sample );
cvFree( (void**) &in.vector );
}
}

void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height )
{
char onevecname[PATH_MAX];
int i = 0;
int filenum = 0;
short tmp;
FILE *info;
CvVecFile outvec;
CvVecFile invec;
int prev_vecsize;

// fopen input and output file
info = fopen( infoname, "r" );
if ( info == NULL )
{
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", infoname );
exit(1);
}
outvec.input = fopen( outvecname, "wb" );
if ( outvec.input == NULL )
{
fprintf( stderr, "ERROR: Output file %s is not writable.\n", outvecname );
exit(1);
}

// Header
rewind( info );
outvec.count = 0;
for ( filenum = 0; ; filenum++ )
{
if ( fscanf( info, "%s", onevecname ) == EOF )
{
break;
}
invec.input = fopen( onevecname, "rb" );
if ( invec.input == NULL )
{
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", onevecname );
exit(1);
}
fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );

outvec.count += invec.count;
if( i > 0 &&  invec.vecsize != prev_vecsize )
{
fprintf( stderr, "ERROR: The size of images in %s(%d) is different with the previous vec file(%d).\n", onevecname, invec.vecsize, prev_vecsize );
exit(1);
}
prev_vecsize = invec.vecsize;
fclose( invec.input );
}
outvec.vecsize = invec.vecsize;
icvWriteVecHeader( outvec.input, outvec.count, outvec.vecsize, 1);

// Contents
rewind( info );
outvec.count = 0;
for ( i = 0; i < filenum ; i++ )
{
if (fscanf( info, "%s", onevecname ) == EOF) {
break;
}
invec.input = fopen( onevecname, "rb" );
fread( &invec.count,   sizeof( invec.count )  , 1, invec.input );
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );
fread( &tmp, sizeof( tmp ), 1, invec.input );

icvAppendVec( invec, outvec, &showsamples, width, height );
fclose( invec.input );
}
fclose( outvec.input );
}

int main( int argc, char **argv )
{
int i;
char *infoname   = NULL;
char *outvecname = NULL;
int showsamples  = 0;
int width        = 24;
int height       = 24;

if( argc == 1 )
{
printf( "Usage: %s\n  <collection_file_of_vecs>\n"
"  <output_vec_filename>\n"
"  [-show] [-w <sample_width = %d>] [-h <sample_height = %d>]\n",
argv[0], width, height );
return 0;
}
for( i = 1; i < argc; ++i )
{
if( !strcmp( argv[i], "-show" ) )
{
showsamples = 1;
// width = atoi( argv[++i] ); // obsolete -show width height
// height = atoi( argv[++i] );
}
else if( !strcmp( argv[i], "-w" ) )
{
width = atoi( argv[++i] );
}
else if( !strcmp( argv[i], "-h" ) )
{
height = atoi( argv[++i] );
}
else if( argv[i][0] == '-' )
{
fprintf( stderr, "ERROR: The option %s does not exist. n", argv[i] );
exit(1);
}
else if( infoname == NULL )
{
infoname = argv[i];
}
else if( outvecname == NULL )
{
outvecname = argv[i];
}
}
if( infoname == NULL )
{
fprintf( stderr, "ERROR: No input file\n" );
exit(1);
}
if( outvecname == NULL )
{
fprintf( stderr, "ERROR: No output file\n" );
exit(1);
}
icvMergeVecs( infoname, outvecname, showsamples, width, height );
return 0;
}

当我编译它时使用:

g++ `pkg-config --cflags opencv` -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp `pkg-config --libs opencv`

有一些警告但没有错误。 但是当我这样运行它时:

sudo sh ./mergevec samples.txt samples.vec

我收到这些错误:

./mergevec: 1: ./mergevec: cannot create �%@@8y@8: Invalid or incomplete multibyte or wide character
./mergevec: 1: ./mergevec: ELF: not found
./mergevec: 2: ./mergevec: �: not found
./mergevec: 1: ./mergevec: Syntax error: Unterminated quoted string

我认为文件编码可能有问题,所以我用 UTF-8 编码保存了 mergevec.cpp,然后用 Windows ISO 编码再次尝试,然后我用 Unix/Linux 行尾保存,然后用 WIndows 行尾保存。我正在使用 gedit。

我做的有什么问题吗?同样的代码在其他机器上编译和执行都很好。我得到了一个在另一台机器上编译的可执行文件,并且在那台机器上运行良好,但是当我在这里运行同一个 .exe 文件时,它给出了同样的疯狂错误。

编辑

我刚刚跑了:

file mergevec

我得到了:

mergevec: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xac5b49d6c194016722ea75ec6f8529578b45d9bc, not stripped

最佳答案

sudo sh ./mergevec samples.txt samples.vec
上面的

sh是一个shell。它期望它的第一个参数是一个 shell 脚本,一个文本文件。

你有一个编译好的可执行文件,所以正确的做法是直接运行它:

sudo ./mergevec samples.txt samples.vec

关于c++ - 在 Ubuntu : "Invalid or incomplete multibyte or wide character", 和有趣的 UTF-8 字符上执行编译文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22940763/

相关文章:

c++ - 为什么连续调用 new[] 不分配连续内存?

python - 单链接聚类

django - sudo apache2 stop 不起作用

Android NDK 构建以支持所有可用设备

ubuntu - 通过 MAKE 将参数传递给编译器

java - 在 ubuntu 10 64 位上安装 java 时出错

c++ - C++ 中按引用长度排列的数组

c++ - 遍历由自定义类组成的列表。我该怎么做? C++

c++ - 对结构列表进行排序

c++ - 在 OpenCV 中复制像素值