由于以文本方式和二進(jìn)制方式讀取回車符,讀取的長度都為為2,而我需要的是字符個(gè)數(shù),下面兩種方法經(jīng)過調(diào)試,并且結(jié)果正確。
第一種方法: 也可以讀取一個(gè)不定長的文件。
FILE *pFile = fopen( pFilePath, \"r\" );
if ( pFile == NULL )
{
return 0;
}
fseek( pFile, 0, SEEK_END );
iFileLen = ftell( pFile );
rewind( pFile );
m_pFileText = new char[iFileLen+1];
fread( m_pFileText, 1, iFileLen, pFile );
m_pFileText[iFileLen] = 0;
fclose( pFile );
第二種方法:
// 計(jì)算字符個(gè)數(shù)
FILE *pFile = fopen( pFilePath, \"r\" );
char ch;
int num = 0;
while ( ch = getc( pFile ) != EOF )
{
num++ ;
}
fclose( pFile );
第一種方法: 也可以讀取一個(gè)不定長的文件。
FILE *pFile = fopen( pFilePath, \"r\" );
if ( pFile == NULL )
{
return 0;
}
fseek( pFile, 0, SEEK_END );
iFileLen = ftell( pFile );
rewind( pFile );
m_pFileText = new char[iFileLen+1];
fread( m_pFileText, 1, iFileLen, pFile );
m_pFileText[iFileLen] = 0;
fclose( pFile );
第二種方法:
// 計(jì)算字符個(gè)數(shù)
FILE *pFile = fopen( pFilePath, \"r\" );
char ch;
int num = 0;
while ( ch = getc( pFile ) != EOF )
{
num++ ;
}
fclose( pFile );