我有一點陣列採用這種結構爲什麼下面不寫數組內容到文件
bit_table_[ROWS][COLUMNS];
我試圖將其寫入文件
FILE * f;
f = fopen("D:\\test_bf\\bf_file", "a+b");
if (f != NULL)
{
fwrite(bit_table_, sizeof(bit_table_), 1, f);
fclose(f);
}
else
{
std::cout<<"File could not be opend.";
printf("\tError %d\t\t%s", errno,strerror(errno));
}
寫作是成功的,但大小該文件爲0kb,其內容爲空。我期望以MB爲單位的文件大小。
我在另一個程序中使用了相同的代碼,並按預期工作。
它有什麼問題?
編輯:
這裏是我實際使用上面的代碼中的函數
void write_to_file(unsigned char bit_table_[][COLUMNS])
{
FILE * f;
f = fopen("D:\\test_bf\\chunk_bf_file.txt", "a+b");
if (f != NULL)
{
fwrite(bit_table_, sizeof(bit_table_), 1, f);
fclose(f);
}
else
{
std::cout<<"File could not be opend.";
printf("\tError %d\t\t%s", errno,strerror(errno));
}
}
這裏是如何它被稱爲:
write_to_file(bit_table_);
這裏是它的聲明:
static unsigned char bit_table_[ROWS][COLUMNS];
你忘記了分機? – Marlon 2012-03-14 09:59:51
這引出了一個問題,「ROWS」和「COLUMNS」的值是什麼? – 2012-03-14 10:02:14
你檢查過來自fwrite的返回值嗎?和錯誤,如果fwrite表示錯誤?這很可能會給你答案。 – harald 2012-03-14 10:03:41