[英]Reading file with visual c++ form behaves differently than reading in C program
我正在使用可視c ++格式構建圖形程序。 我正在嘗試將文件讀取為字符串。 該文件的內容是簡單的html代碼。
現在,如果我創建一個空白項目並使用以下代碼創建一個.c文件:
FILE *f;
int tamanho;
char *asd;
f=fopen("mail.txt","r");
if(f==NULL)
erro("Erro abrir file");
fseek(f,0,SEEK_END);
tamanho=ftell(f);
rewind(f);
asd=(char *)malloc(tamanho+1);
fread(asd,1,tamanho,f);
它將整個復制到字符串。
但是,如果我創建Windows窗體應用程序並編寫相同的代碼,則它只會復制文件的幾行。
fread()不能保證讀取您要求的所有內容。
您需要檢查返回值以查看實際讀取了多少。
您可能需要循環執行此操作,直到您閱讀了所有想要的內容。
size_t read = 0;
while(read != tamanho)
{
size_t amount = fread(asd + read,1,tamanho - read,f);
if (amount == 0)
{ // You may want to check for read errors here
}
read += amount;
}
缺少一會兒循環嗎? 這樣,您可以確保正確到達文件末尾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.