0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C言語】フォルダの存在チェックして存在しない時作成する win7 32bit

Last updated at Posted at 2014-07-28
001.c
# include <stdio.h>
# include <dirent.h>
# include <sys/stat.h>

int main ()
{

    struct stat buf;
    int ret;
    char dir[256];
    char mkdir[512];

    snprintf(dir,256,"sample");
    snprintf(mkdir,512,"mkdir %s",dir);

    ret=stat(dir, &buf);

    if(ret!=0){

        ret=system("dir");

        if(ret==0){

            ret=system(mkdir);

            if(ret==0){

                printf("\n\n");
                printf("%sフォルダ作成成功! \n ",dir);
                printf("\n\n ");

                ret=system("dir");

                if(ret!=0){
                    printf("dirコマンド失敗! \n ");
                }

            }else{
                printf("%sフォルダ作成失敗! \n ",dir);
            }

        }else{
            printf("dirコマンド失敗! \n ");
        }

    }else{
        printf("%sフォルダが存在します \n ",dir);
    }

    return 0;
}

環境

OS:windows7 32bit (自作機)
コンパイラ:Borland C++ 5.5.1 for Win32

解説

sampleフォルダが存在しないときにsampleフォルダを生成する。
statでフォルダの存在チェックして、system関数でmkdirコマンド実行してフォルダ作成を行った。

# include <dirent.h>

がないとき、system関数で警告が出る。

c:\2014\0726>bcc32 001.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
001.c:
警告 W8065 001.c 20: プロトタイプ宣言のない関数 'system' の呼び出し(関数 main )
警告 W8065 001.c 24: プロトタイプ宣言のない関数 'system' の呼び出し(関数 main )
警告 W8065 001.c 32: プロトタイプ宣言のない関数 'system' の呼び出し(関数 main )
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

コンパイルと実行結果

上記ソースを
c:\2014\0726\001.c
としてコンパイルして実行する。

c:\2014\0726>bcc32 001.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
001.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

c:\2014\0726>001.exe
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は XXXXXXXXXX です

 c:\2014\0726 のディレクトリ

2014/07/28  18:58    <DIR>          .
2014/07/28  18:58    <DIR>          ..
2014/07/28  18:58               924 001.c
2014/07/28  18:58            60,928 001.exe
2014/07/28  18:58             4,380 001.obj
2014/07/28  18:58           393,216 001.tds
2014/07/27  05:50             2,523 002.c
               5 個のファイル             461,971 バイト
               2 個のディレクトリ  14,715,723,776 バイトの空き領域


sampleフォルダ作成成功!


  ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は XXXXXXXXXX です

 c:\2014\0726 のディレクトリ

2014/07/28  18:58    <DIR>          .
2014/07/28  18:58    <DIR>          ..
2014/07/28  18:58               924 001.c
2014/07/28  18:58            60,928 001.exe
2014/07/28  18:58             4,380 001.obj
2014/07/28  18:58           393,216 001.tds
2014/07/27  05:50             2,523 002.c
2014/07/28  18:58    <DIR>          sample
               5 個のファイル             461,971 バイト
               3 個のディレクトリ  14,715,723,776 バイトの空き領域

c:\2014\0726>

関連

[【C言語】systemでdirコマンドを実行する win7 32bit ][1]
[1]:http://qiita.com/kyoshiro-obj/items/489be634d9654fc9082b

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?