函数getline错误MFC vs2012(msvcp110.dll)(Getline error

2019-08-02 22:34发布

我必须在vs2012与MFC应用程序中使用的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产生:

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

和VS11重定向我到xiosbase线443

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

对于我使用的项目属性“使用MFC共享DLL”和“多线程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);
            }
        }
    }

任何人的想法是怎么回事?

Answer 1:

我以VS10现在解决了这个问题。 在那里,算法的工作没有任何问题。 但我不认为这可能是解决方案!

它与在同一台PC上VS10的作品告诉我,还在于它是不是与PC的一个问题。



Answer 2:

When you open a file object you should always make sure it's valid before trying to use it.

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

P.S. You have two objects named file in the same block of code. Please don't do that, it's potentially confusing even if the compiler is able to keep it straight.



Answer 3:

首先,让我们简化问题:

只是硬编码的路径,并在新的控制台项目运行它:(我增加了更多的保护代码)

 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 } 



Answer 4:

也许这是不写这之前包含在文件路径字符串中的行内容是一个好主意。 尝试以下方法:

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

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

顺便说一句,你复制和粘贴代码? 正如你写道:

usign空间std;

使用命名空间std代替;



Answer 5:

这是工作,是不会了。

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

这应该在2012年的VisualStudio至少工作。



文章来源: Getline error MFC vs2012 (msvcp110.dll)