繁体   English   中英

Getline错误MFC vs2012(msvcp110.dll)

[英]Getline error MFC vs2012 (msvcp110.dll)

我在使用MFC应用程序在vs2012中使用std :: getline函数时遇到问题。 vs2010中运行的是相同的代码,这就是为什么我确定代码本身没有问题的原因。

void AddImage::OnClickedIdbAiRegistration(){
CFileDialog file(TRUE, NULL, NULL, OFN_OVERWRITEPROMPT, "(*.dat)|*.dat||");
file.DoModal();
UpdateData();
m_ai_file=file.GetPathName();
UpdateData(FALSE);
std::string buf=m_ai_file;
if(filecnt(buf, "Dat")){
    std::ifstream file(buf);
    AfxMessageBox(buf.c_str());
    std::getline(file, buf);//Here is my problem
    AfxMessageBox(buf.c_str());
    file.close();
    }
}

第一个AfxMessageBox返回文件路径(正确和有效的ASCII文件)。 我没有到达第二个AfxMessageBox,因为getline产生了:

在program.exe中的0x000007FEF7B4AAEE(msvcp110.dll)处未处理的异常:0xC0000005:访问冲突读取位置0xFFFFFFFFFFFFFFFFFF。

和vs11将我重定向到xiosbase行443

    locale __CLR_OR_THIS_CALL getloc() const
    {   // get locale
    return (*_Ploc);/*THIS IS LINE 443*/
    }

对于项目属性,我正在使用“在共享dll中使用MFC”和“多线程DLL”以及子系统“ Windows”

附加程序代码,包括:

#include <afxwin.h>
#include <afxframewndex.h>
#include <afxcmn.h>
#include <afxdialogex.h>

#include <iostream>
#include <string>
#include <sstream>
#include <regex>
#include <fstream>
#include <time.h>
#include <Windows.h>

usign namespace std;

class AddImage:public CDialog{
        DECLARE_DYNAMIC(AddImage)
    public:
        AddImage(CWnd* pParent = NULL);
        virtual ~AddImage();
        enum {IDD=IDD_ADD_IMAGE};
    protected:
        virtual void DoDataExchange(CDataExchange* pDX);
        DECLARE_MESSAGE_MAP()
    public:
        CString m_ai_file;
    };

AddImage::AddImage(CWnd* pParent):CDialog(AddImage::IDD, pParent){
    m_ai_file=_T("");
    }

AddImage::~AddImage(){
    }



bool filecnt(string path, string type){
    if(filepathcnt(path, type)){
        if(GetFileAttributes(path.c_str())==-1){
            return(FALSE);
            }
        else{
            return(TRUE);
            }
        }
    else{
        return(FALSE);
        }
    }

bool filepathcnt(string path, string type){
    if(type==""){
        tr1::regex regex("[[:print:]]+\\.[[:alnum:]]+");
        if(regex_match(path.begin(), path.end(), regex)){
            return(TRUE);
            }
        else{
            return(FALSE);
            }
        }
    else if(type=="-"){
        tr1::regex regex("[[:print:]]+");
        if(regex_match(path.begin(), path.end(), regex)){
            return(TRUE);
            }
        else{
            return(FALSE);
            }
        }
    else{
        string upper=type;
        string lower=type;
        transform(upper.begin(), upper.end(), upper.begin(), toupper);
        transform(lower.begin(), lower.end(), lower.begin(), tolower);
        tr1::regex norm_regex("[[:print:]]+\\."+type);
        tr1::regex upper_regex("[[:print:]]+\\."+upper);
        tr1::regex lower_regex("[[:print:]]+\\."+lower);
        if(regex_match(path.begin(), path.end(), upper_regex) || regex_match(path.begin(), path.end(), lower_regex) || regex_match(path.begin(), path.end(), norm_regex)){
            return(TRUE);
            }
        else{
            return(FALSE);
            }
        }
    }

任何人都知道出了什么问题吗?

我现在已经通过使用VS10解决了这个问题。 在那里,该算法可以正常工作。 但是我不认为这可以解决!

它可以在同一台PC上与vs10一起使用,这也告诉我这不是PC的问题。

当您打开文件对象时,应始终确保该文件有效,然后再尝试使用它。

if (file.bad())
    AfxMessageBox("Bad file");
else
{ // existing code follows

PS您在同一代码块中有两个名为file对象。 请不要这样做,即使编译器能够保持其正确性,也可能造成混乱。

首先让我们简化一下问题:

只需对路径进行硬编码并在新的控制台项目中运行它:(我添加了更多保护代码)


    String buf="the data file path";
    std::ifstream file(buf);
    if(!file.is_open())
        return FALSE;
    while(file.good())
    {
        cout << buf << endl;
        std::getline(file, buf);
        cout << buf << endl;
    }
    file.close();

结果是什么? 如果问题仍然存在,则可以在第二个cout << buf << endl处设置调试点,以检查buf是否分配了值。

您还应该按以下方式保护FileDialog:或者单击“取消”,它将失败。


(dlg.DoModal()==IDOK)
  FilePathName=dlg.GetPathName();
  //....other opertiaon
} 

将行内容写入之前包含文件路径的字符串中也许不是一个好主意。 请尝试以下操作:

CString csFilePath("C:\\example.txt");
ifstream infile;
infile.open(csFilePath);

std::string strLine;
std::getline(infile, strLine);

顺便说一句,您是否复制并粘贴了代码? 如您所写:

usign名称空间std;

而不是使用命名空间std;

它正在工作,现在不再。

对于调试和发布配置,将编译标志(在项目属性>配置属性> C ++>代码生成>运行时库中)从/MD (多线程DLL)更改为/MDd (多线程调试DLL)。

这至少应该在VisualStudio 2012中起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM