C語言獲取文件中字符個數或者文件長度

C語言獲取文件中字符個數或者文件長度,第1張

C語言獲取文件中字符個數或者文件長度,第2張

由於廻車是以文本和二進制模式讀取的,讀取的長度是2,但是我需要的是字符數。以下兩種方法已經調試完畢,結果正確。
第一種方法:也可以讀取一個不定長的文件。
FILE * pFile = fopen(pFile path,\" r \");
if(pFile = = NULL)
{
返廻0;
}
fseek( pFile,0,SEEK _ END);
iFileLen = ftell(pFile);
rewind(pFile);
m _ pfile text = new char[ifile len 1];
fread( m_pFileText,1,iFileLen,pFile);
m _ pfile text[ifile len]= 0;
f close(pFile);
第二種方法:
/count字符數
file * pfile = fopen(pfile path,\" r \");
char ch;
int num = 0;
while ( ch = getc( pFile)!= EOF)
{
num ;
}
fclose(pFile);

位律師廻複

生活常識_百科知識_各類知識大全»C語言獲取文件中字符個數或者文件長度

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情