我正在嘗試使用該函式存盤檔案的第一個4 chars.awvstd::fgetc
這就是我所擁有的
FILE* WAVF = fopen(FName, "rb");
std::vector<std::string> ID;
ID[4];
for (int i = 0; i < 4; i )
{
ID[i] = fgetc(WAVF);
}
我不斷收到此錯誤:
Exception thrown at 0x00007FF696431309 in ConsoleApplication3.exe:
0xC0000005: Access violation writing location 0x0000000000000010.
uj5u.com熱心網友回復:
您的程式有未定義的行為!
你的向量ID是空的。通過呼叫operator[]一個空的std::vector,呼叫一個未定義的行為。你很幸運,你的程式崩潰了,說“訪問沖突”。
你需要:
// create a vector of string and initialize 4 empty strings
std::vector<std::string> ID(4);
for (auto& element: ID)
{
element = some `std::string`s
}
但是,在您的情況下,std::fgetc回傳intger 作為
成功或
EOF失敗時獲得的字符。
因此,您可能需要諸如std::vector<char>or (充其量)之類的資料結構std::string。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/339715.html
下一篇:具有可變數量引數的建構式
