소프트웨어

[c++/c] linux환경, 파일, 폴더 생성 소스 포스팅

개발자_이훈규 2014. 2. 25. 17:17

1.

그냥그냥 모아놓으면 어차피 내 소스니깐 내가 쓰기 편해서 지속적으로 올려 놓을 생각이다.


2. 

키워드 : 파일, 폴더, 생성, 소스, 코드, file, fopen


3.


total.cpp


#include <stdio.h>                                                              

#include <sys/types.h>                                                          

#include <sys/stat.h>                                                           

#include <unistd.h>                                                                                       

                                                                                                          

int main() {                                                                                              

                                                                                                          

                                                                                                          

    //*********************************                                                                   

    // create directory                                                                                   

    //*********************************                                                                   

    struct stat st = {0};                                                                                 

    if( stat("com", &st) == -1 ) {                                                                        

        mkdir("com", 0700);                                                                               

    }                                                                                                     

    else                                                                                                  

        printf("fail\n");                                                                                 

                                                                                                          

                                                                                                          

    //*********************************                                                                   

    // create file                                                                                        

    //*********************************                                                                   

    FILE *fp;                                                                                             

    char path[100] = "./a";                                                                               

    char character[100] = "hello world\n";                                                                

    //char path[100] = "~\\folder\\t";                                                                    

                                                                                                          

    fp = fopen(path, "wb");                                                                               

    fwrite(character, sizeof(char), sizeof(character),fp);                                                

                                                                                                          

    fclose(fp);                                                                                           

                                                                                                          

    //*********************************                                                                   

    // print current working directory                                                                    

    //*********************************                                                                   

    char cwd[1024];                                                                                       

    getcwd(cwd, sizeof(cwd));                                                                             

    printf("current working directory \n");                                                               

    printf("%s\n", cwd);                                                                                  

                                                                                                          

    return 0;                                                                                             

} 



tip.

fwrite는 thread safe하지 않다.