char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
FILE *f;
f= fopen( "TIMES.txt", "wb");
if (f == NULL)
{
printf("Error! Could not open file\n");
exit(-1);
}
int i,j;
for(i=0;i<3;i )
{
for(j=0;j<7;j )
{
printf("%c",arrTypeLabels[i][j]);
fwrite(arrTypeLabels[i][j],sizeof(char),sizeof(arrTypeLabels),f);
}
}
fclose(f);aenter code here
我打開了 TIMES.txt 檔案,但我看不到任何輸出,雖然我認為我的代碼是正確的 ..................... :/ 請幫助...
uj5u.com熱心網友回復:
char arrTypeLabels[3][7] = {
{"Random"},
{"ASC"},
{"DESC"}
};
FILE *f = fopen("TIMES.txt", "wb"); //wb is OK
if (f == NULL)
{
printf("Error! Could not open file\n");
exit(-1);
}
int i, j;
for (i = 0; i < 3; i )
{
for (j = 0; j < 7; j )
{
printf("%c", arrTypeLabels[i][j]);
fwrite(arrTypeLabels[i] j, sizeof (char), sizeof (char), f); //your mistake is here
}
}
fclose(f);
我不知道你是如何編譯你的代碼的,因為在 中fwrite,第一個引數需要是一個指標,或者在你的代碼中,你給出了值。
此外,您嘗試做的事情令人困惑,因為看起來您正在嘗試寫入charby char,但您嘗試寫入包含arrTypeLabels在一次fwrite呼叫中的全部資料。
uj5u.com熱心網友回復:
如果您只想將該陣列寫入檔案,則可以執行以下操作:
char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
FILE *f;
f= fopen( "TIMES.txt", "wb");
if (f == NULL)
{
printf("Error! Could not open file\n");
exit(-1);
}
fwrite(arrTypeLabels,sizeof(char),sizeof(arrTypeLabels),f);
fclose(f);
或類似的東西:
char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
FILE *f;
f= fopen( "TIMES.txt", "wb");
if (f == NULL)
{
printf("Error! Could not open file\n");
exit(-1);
}
for (int i = 0; i < 3; i )
{
for (int j = 0; j < 7; j )
fwrite(arrTypeLabels[i] j,sizeof(char),sizeof(char),f);
}
fclose(f);
請注意,第一種方法要快得多,因為您不必一次寫入(寫入檔案,即硬碟驅動器)每個字符。
uj5u.com熱心網友回復:
fwrite第一個和第三個引數是錯誤的。由于您想按字符寫入字符,因此您的行應該是fwrite(&buf[i][j], 1,1,f);
或者更簡單,使用fputc:
fputc(buf[i][j], f);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/393804.html
上一篇:從Jar中提取時的聲音失真
