ファイルの行数は、ファイルを最後まで読み込んで初めて分かります、例えば以下のプログラムは
『ファイルを読み込みながら文字列を画面に描画する』プログラムですが、行数をカウントしている
LineCounter がファイル全体の行数になるのはファイルを全部読み込み終わってからです
#include "DxLib.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
int LineCounter ;
char StringBuffer[ 2048 ] ;
int FileHandle ;
// ウインドウモードで起動
ChangeWindowMode( TRUE ) ;
// DXライブラリの初期化
if( DxLib_Init() < 0 ) return -1 ;
// 描画先を裏画面に変更
SetDrawScreen( DX_SCREEN_BACK ) ;
// ファイルをオープン
FileHandle = FileRead_open( "Test.txt" ) ;
// 読み込みながら全行を画面に描画する
LineCounter = 0 ;
while( FileRead_eof( FileHandle ) == 0 ) // ファイルの終端に到達するまでループ
{
// 1行読み込む
FileRead_gets( StringBuffer, sizeof( StringBuffer ), FileHandle ) ;
// 読み込んだ行を描画する
DrawString( 0, LineCounter * 16, StringBuffer, GetColor( 255,255,255 ) ) ;
// 行数を1増やす
LineCounter ++ ;
}
// 行数を描画する
DrawFormatString( 0, LineCounter * 16, GetColor( 255,255,255 ), "行数 : %d", LineCounter ) ;
// 裏画面の内容を表画面に反映する
ScreenFlip() ;
// キー入力待ち
WaitKey() ;
// DXライブラリの後始末
DxLib_End() ;
// ソフトの終了
return 0 ;
}
なので、行数だけ取得されたい場合は上記のプログラムから文字列を描画している処理を削除して
以下のようにすることで行数を取得することができます
#include "DxLib.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
int LineCounter ;
char StringBuffer[ 2048 ] ;
int FileHandle ;
// ウインドウモードで起動
ChangeWindowMode( TRUE ) ;
// DXライブラリの初期化
if( DxLib_Init() < 0 ) return -1 ;
// 描画先を裏画面に変更
SetDrawScreen( DX_SCREEN_BACK ) ;
// ファイルをオープン
FileHandle = FileRead_open( "Test.txt" ) ;
// 読み込みながら行数を数える
LineCounter = 0 ;
while( FileRead_eof( FileHandle ) == 0 ) // ファイルの終端に到達するまでループ
{
// 1行読み込む
FileRead_gets( StringBuffer, sizeof( StringBuffer ), FileHandle ) ;
// 行数を1増やす
LineCounter ++ ;
}
// 行数を描画する
DrawFormatString( 0, 0, GetColor( 255,255,255 ), "行数 : %d", LineCounter ) ;
// 裏画面の内容を表画面に反映する
ScreenFlip() ;
// キー入力待ち
WaitKey() ;
// DXライブラリの後始末
DxLib_End() ;
// ソフトの終了
return 0 ;
}