C语言获取文件中字符个数或者文件长度
点击次数:39 次 发布日期:2008-12-01 12:46:48 作者:源代码网
|
第一种方法: 也可以读取一个不定长的文件。 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 ); 第二种方法: // 计算字符个数 FILE *pFile = fopen( pFilePath, "r" ); char ch; int num = 0; while ( ch = getc( pFile ) != EOF ) { num++ ; } fclose( pFile ); 源代码网供稿. |
