_findfirst、_findnext、_findclose

指定したパス以下のファイルを検索するプログラム


#pragma warning(disable:4786)
#include <io.h>
#include <vector>
#include <string>
using namespace std;

typedef vector<string> _dirque;

//----------------------------------------------------------
void Search( _dirque &que )
{
 // このサブルーチンはキューにパスが入っている限り呼ばれつづける
 // _finddata_t構造体は_findfirstおよび_findnextが返すファイル属性情報を格納する

 _finddata_t findData;
 long hFile; // 戻り値用
 _dirque::iterator it; // イテレータ定義

 string dirPath = que.at(0); // キューの先頭を取得
 it = que.begin(); // 先頭を取得
 que.erase(it); // 先頭を詰める

 string targetPath = dirPath + "\\*.*"; // ディレクトリも含めて検索
 printf( "検索中 ⇒ %s\n", targetPath.c_str() );

 if ( ( hFile = _findfirst( targetPath.c_str(), &findData ) ) == -1L ) {
  printf( "Not found in target directory\n" );
  return;
 }
 while ( _findnext( hFile, &findData ) == 0 ) {
  // 1文字目が"."".."とかは無視して続行
  if( findData.name[0] == '.' ) continue;

  // 見つかった名前を連結して新しいパスを作成
  string newPath = dirPath + "\\" + findData.name;

  if ( findData.attrib & _A_SUBDIR ) {
   que.push_back( newPath ); // 検索パスを追加
   printf( "フォルダが見つかりました ⇒ %s\n", newPath.c_str() );
  } else {
   printf( "ファイルが見つかりました ⇒ %s\n", newPath.c_str() );
  }
 }
 _findclose( hFile );

}

//----------------------------------------------------------
int main( int argc, char **argv )
{
 // 引数チェック
 if ( argc != 2 ) {
  printf( "引数の数が正しくありません\n" );
  exit( 1 );
 }

 printf( "検索開始パス ⇒ %s\n", argv[1] );
 _dirque que; // キュー配列を定義
 que.push_back( argv[1] );

 // 検索処理
 while ( que.size() )
  Search( que );

 return 0;
}

Windows > システムコール | comments (0) | trackbacks (0)

ファイルから1行ずつ読み込む/書き込む


#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
 FILE *fpIn, *fpOut;
 char str[256];

 // 引数チェック
 if ( argc != 3 ) {
  printf( "引数の数が違います\n" );
  exit( 1 );
 }

 // ファイルオープン
 if ( ( fpIn = fopen( argv[1], "r" ) ) == NULL ) {
  printf( "入力ファイルがオープンできません\n" );
  exit( 1 );
 }
 if ( ( fpOut = fopen( argv[2], "w" ) ) == NULL ) {
  printf( "出力ファイルがオープンできません\n" );
  exit( 1 );
 }

 // メイン処理
 while ( ( fgets( str, 256, fpIn ) ) != NULL )
  fputs( str, fpOut );

 // ファイルクローズ
 fclose( fpIn );
 fclose( fpOut );
}


fgetsは改行を取り除かずに入力し、
fputsは改行を付けずに出力します。

改行文字を削除するには

str[ strlen( str ) - 1 ] = '\0';
または
*( str + strlen( str ) -1 ) = '\0';


とします。
C > 標準関数 > ファイル入出力関数 | comments (0) | trackbacks (0)

ファイルから1データずつ読み込む/書き込む


#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
 FILE *fpIn, *fpOut;
 int data, sum = 0;

 // 引数チェック
 if ( argc != 3 ) {
  printf( "引数の数が違います\n" );
  exit( 1 );
 }

 // ファイルオープン
 if ( ( fpIn = fopen( argv[1], "r" ) ) == NULL ) {
  printf( "入力ファイルがオープンできません\n" );
  exit( 1 );
 }
 if ( ( fpOut = fopen( argv[2], "w" ) ) == NULL ) {
  printf( "出力ファイルがオープンできません\n" );
  exit( 1 );
 }

 // メイン処理
 while ( ( fscanf( fpIn, "%d", &data ) ) != EOF )
  sum += data;

 fprintf( fpOut, "合計:%d", sum );

 // ファイルクローズ
 fclose( fpIn );
 fclose( fpOut );
}

C > 標準関数 > ファイル入出力関数 | comments (0) | trackbacks (0)

ファイルから1文字ずつ読み込む/書き込む


#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
 FILE *fpIn, *fpOut;
 int c;

 // 引数チェック
 if ( argc != 3 ) {
  printf( "引数の数が違います\n" );
  exit( 1 );
 }

 // ファイルオープン
 if ( ( fpIn = fopen( argv[1], "r" ) ) == NULL ) {
  printf( "入力ファイルがオープンできません\n" );
  exit( 1 );
 }
 if ( ( fpOut = fopen( argv[2], "w" ) ) == NULL ) {
  printf( "出力ファイルがオープンできません\n" );
  exit( 1 );
 }

 // メイン処理
 while ( ( c = getc( fpIn ) ) != EOF )
  fputc( c, fpOut );

 // ファイルクローズ
 fclose( fpIn );
 fclose( fpOut );
}

C > 標準関数 > ファイル入出力関数 | comments (0) | trackbacks (0)