0
後訪問衝突以下功能工作正常,顯示由行的文本文件一行到標準錯誤:使用fstreams拋出主要收益
void load_text(std::string path){
ifstream f(path);
char linebuff[100];
linebuff[99] = 0;
while(!f.eof()){
f.getline(linebuff, 99);
std::cerr<<linebuff<<std::endl;
}
f.close();
}
現在,當主函數返回時,它拋出以下存取權限衝突錯誤:
app.exe中0x77e58dc9未處理的異常:0xC0000005:訪問衝突寫入位置0x00000014。
奇怪的是,創建一個ifstream的,關閉它並返回引發錯誤
//This also crashes when returning from main
void load_text(std::string path){
ifstream f(path);
f.close();
}
任何想法,爲什麼出現這種情況?
編輯:
主要功能(因爲它是編譯),這實際工作只要你創建一個新的項目,與實際程序的區別有很多從來沒有所謂的,從未使用過的功能和類
現在我在「無法重現」的階段:
#include <fstream>
#include <string>
#include <iostream>
//Using SDL for plotting
#ifdef WIN32
#pragma comment(lib, "SDL")
#pragma comment(lib, "SDLMain")
#pragma comment(lib, "SDL_image")
#endif
int fn(std::string path){
std::ifstream f(path);
char linebuff[100];
linebuff[99] = 0;
while(!f.eof()){
f.getline(linebuff, 99);
std::cerr<<linebuff<<std::endl;
}
f.close();
return 0;
}
int main(int argc, char** argv){
fn("sscce.cpp");
return 0;
}
[(!EOF())。''同時是錯誤的(http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-:
通過發現條件考慮的,是錯誤的)。你爲什麼用C字符串閱讀? – chris
除了與我要做的事情('std :: string line'和'while(std :: getline(f,line))'有所不同的是,對於循環條件,'f.eof()'幾乎不會你想做什麼),這看起來很好(特別是後者)。你說這個異常是在main()返回後,但是不要在這個proc調用的任何一邊顯示main()中的內容。請發佈一個[SSCCE](http://www.sscce.org),其中包含一個重現問題的main()? – WhozCraig
試圖重現一個示例程序。主要與編輯時提供的主要相同,功能也是如此,但這不會引發任何錯誤。 #pragmas可以被刪除。 – NeonMan