2014-11-04 27 views
1

我在嘗試加載文本文件時遇到訪問衝突寫入位置錯誤。在調試的時候,我注意到我的「is_open()」和「good()」檢查了這兩個通過,因爲我達到了「while(std :: getline(myfile,line))。這怎麼可能?甚至是笨重的,功能完美地工作在自己的項目,但由於某種原因,我在這裏得到了訪問衝突錯誤。無法讀取文本文件還沒有通過is_open和良好的檢查?

//頭

static bool LoadObj(std::string file, std::vector<GLfloat> &out_vertices, std::vector<GLfloat> &out_normals); 

// CPP

bool Resources::LoadObj(std::string file, std::vector<GLfloat> &out_vertices, std::vector<GLfloat> &out_normals) 
{ 
std::string line; 
std::ifstream myfile(file); 

if (myfile.is_open()) 
{ 
    if (myfile.good()) 
    { 
     while (std::getline(myfile, line)) 
     { 
      if (!strncmp(line.c_str(), "v", 1)) 
      { 
       std::string dummy; 
       std::stringstream ss(line); 
       ss >> dummy; 

       while (ss >> line) 
       { 
        out_vertices.push_back(std::stof(line)); 
        std::cout << line; 
       } 
      } 
     } 
    } 
} 

return false; 
} 
+0

訪問衝突與打開或未打開的文件無關。這個錯誤很可能在別的地方。 – 2014-11-04 03:27:05

+0

輸入文件上的「寫入位置」錯誤? – 2014-11-04 03:29:14

+1

你正在寫給記憶你不應該。抓住一個調試器,看看它正在發生哪一行,並查看你正在濫用什麼指針以及如何操作。 – 2014-11-04 03:31:18

回答