循环中,ifstream读取前几个文件正常,到第9个文件出现异常中断

cuiruiff 2015-06-23 04:50:32
一个图像匹配的项目,在xcode下开发,运行无误,移植到vs 2012下之后出问题




关于图片匹配中要用到的相关信息 存在txt文件中,在批量匹配过程中
利用以下的两个函数读取txt文件中内容到程序中
但是在循环中,前几幅图片的关联txt文件读取正常,到了第九幅图片便在ifstream函数处出现上面图片中的这个错误信息



这幅图片是程序的输出结果
输出结果中,每两行对应一幅图片的读取(以及匹配过程)
第一行是在ifstream之前打印出需要被读取的txt文件的路径,第二行是打印出从txt文件中读取的相关信息,其中的乱码是关于图片中相关目标的名字(中文,记录在txt 文件中,写入文件的时候用的是char[]类型保存了该名称),在xcode下的时候显示是正常的,此处显示错误不知道是不是字符集的问题

以下是代码:



for(int i = 0 ; i < testImgsNumber; i ++){//对每张图片进行测试

for(int j = 0 ; j < clusterNumber; j++) {
flag[j] = 0;
}
string imgPath = testFiles.getFileName(i);
Mat img = imread(imgPath);
vector<KeyPoint> keypoint;
detector->detect(img,keypoint);
Mat TF = countWordsFreq(img, keypoint, NULL, bowDE);//获得传入的图片的TF

Mat tfIdfOfImg(1,clusterNumber,CV_32F);

for(int i = 0 ; i < TF.cols; i ++ ){//计算传入的图片的tf-idf向量
tfIdfOfImg.at<float>(0,i) = idfMat.at<float>(0,i)*TF.at<float>(0,i);
// cout << TF.at<float>(0,i) << " ";
}
struct invertedTableNode image_from_user;
char txtName[512];

sprintf(txtName, "%s%s",imgPath.substr(0,imgPath.rfind(".JPEG")).c_str(),".txt");
cout << txtName << endl;

getInfoFromTxt(txtName, image_from_user);//--------------------------这句话出错

。。。。。



/**
1.打开文件
2.文件内容读入字符串
3.截取字符串内容,存入相应变量
*/

void getInfoFromTxt(string txtName, struct invertedTableNode& image){//读入图片对应的txt文件内容

string data;//存储文件内容的string

if(readFileToString(txtName.c_str(), data))
。。。。。。
}


/**
文件内容读入字符串
file_name:所要读取内容的文件名
fileData:存储文件内容的字符串
*/
bool readFileToString(const char* file_name, string& fileData)
{
ifstream file(file_name, std::ifstream::binary);

if(file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
char* file_buf = new char [file_size+1];
//make sure the end tag \0 of string.

memset(file_buf, 0, file_size+1);

// Read the entire file into the buffer.
file.seekg(0, ios::beg);
file.read(file_buf, file_size);


if(file)
{
fileData.append(file_buf);
}
else
{
std::cout << "error: only " << file.gcount() << " could be read";
fileData.append(file_buf);
return false;
}
file.close();
//delete file_name;
delete file_buf;
}
else
{
//delete file_name;
file.close();
return false;
}


return true;
}
...全文
230 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-06-24
  • 打赏
  • 举报
回复
看不懂时双击下一行,直到能看懂为止。 如果逐行双击直到最下面一行,对应源代码处全都看不懂的话,参考下面: 有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
cuiruiff 2015-06-23
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
谢谢你的帮助,你帮我找到出错的位置,但是,出错位置我明白,就是调用ifstream时出的错,我还是不明白出错怎么解决的?
赵4老师 2015-06-23
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧