유닉스에서 파일 목록 출력하기

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <error.h>
 
int main()
{
    DIR *dir;
    struct dirent *ent;
    dir = opendir ("./");
    if (dir != NULL) {
 
    /* print all the files and directories within directory */
    while ((ent = readdir (dir)) != NULL) {
        printf ("%s\n", ent->d_name);
    }
    closedir (dir);
    } else {
         /* could not open directory */
         perror ("");
        return EXIT_FAILURE;
    }
}


윈도우에서 파일 목록 출력하기

#include <stdio.h>
#include <io.h>
#include <conio.h>
 
void main()
{
    _finddata_t fd;
    long handle;
    int result = 1;
    handle = _findfirst(".\\*.*", &fd);  //현재 폴더 내 모든 파일을 찾는다.
 
    if (handle == -1)
    {
        printf("There were no files.\n");
        return;
    }
 
    while (result != -1)
    {
        printf("File: %s\n", fd.name);
        result = _findnext(handle, &fd);
    }
 
    _findclose(handle);
    return;
}

출처 링크, 링크


문제점

Microsoft Visual Studio 2010 컴파일시 에러 발생

"COFF로 변환하는 동안 오류가 발생했습니다. 파일이 잘못되었거나 손상되었습니다."


해결방안

프로젝트 속성 > 구성 속성 > 링커 > 일반 > 중분 링크 사용 > 아니요(/INCREMENTAL:NO)
혹은
프로젝트 속성 > 구성 속성> 매니페스트 도구 > 입력 및 출력 > 매니페스트 포함 > 아니요
영구적으로 해결하는 방법
-> Visual Studio 서비스팩 1 다운로드



 실행환경

 Desktop

 조립식

 CPU

 Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz 3.40GHz

 Memory

 8.00 GB

 OS

 Windows 7 Professional K 64bit

 Java

 1.8.0_05

 MySQL

 Ver 14.14 Distrib 5.6.19, for Win64

 Web Server

 Apache Tomcat 7.0.51


문제점

컴파일시 아래와 같은 에러 중 어느 한가지라도 발생했을 경우.(확실치는 않다.)

Warning    2    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    336    1    Programa 3205
Warning    3    warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    363    1    Programa 3205
Warning    4    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    369    1    Programa 3205
Warning    5    warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    371    1    Programa 3205
Warning    6    warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    374    1    Programa 3205
Warning    7    warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    375    1    Programa 3205
Warning    9    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    718    1    Programa 3205
Warning    10    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    727    1    Programa 3205
Warning    11    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    729    1    Programa 3205
Warning    12    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    754    1    Programa 3205
Warning    13    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    766    1    Programa 3205
Warning    14    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    768    1    Programa 3205
Warning    15    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    792    1    Programa 3205
Warning    16    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    806    1    Programa 3205
Warning    17    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    808    1    Programa 3205
Warning    18    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    830    1    Programa 3205
Warning    19    warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    842    1    Programa 3205
Warning    20    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    844    1    Programa 3205
Warning    21    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    866    1    Programa 3205
Warning    22    warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    911    1    Programa 3205
Warning    23    warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    f:\fernando luiz\teste-3205\ps3000.c    939    1    Programa 3205

해결방안


Project properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions > _CRT_SECURE_NO_WARNINGS
프로젝트 오른쪽 클릭 > 속성 > 구성 속성 > C/C++ > 전처리기 > 전처리기 정의 > _CRT_SECURE_NO_WARNINGS 추가.

보안상의 문제로 다른 함수를 사용하라는 것인데 설정 추가로 해결할 수 있다.

 실행환경

 Desktop

 조립식

 CPU

 Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz 3.40GHz

 Memory

 8.00 GB

 OS

 Windows 7 Professional K 64bit

 Java

 1.8.0_05

 MySQL

 Ver 14.14 Distrib 5.6.19, for Win64

 Web Server

 Apache Tomcat 7.0.51


문제점

유닉스에서 사용하던 함수 open(), close(), write(), read()를 window Visual Studio 에서 사용하려하는데 에러 발생.

해결방안

_open()을 사용한다. 다른 함수 모두 앞에 '_'를 붙혀 사용한다. 아래 코드는 MS Developer Network C 라이브러리에서 참고하였다.

#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <io.h> #include <stdio.h> int main( void ) { int fh1, fh2; fh1 = _open( "CRT_OPEN.C", _O_RDONLY ); // C4996 // Note: _open is deprecated; consider using _sopen_s instead if( fh1 == -1 ) perror( "Open failed on input file" ); else { printf( "Open succeeded on input file\n" ); _close( fh1 ); } fh2 = _open( "CRT_OPEN.OUT", _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE ); // C4996 if( fh2 == -1 ) perror( "Open failed on output file" ); else { printf( "Open succeeded on output file\n" ); _close( fh2 ); } }


참고 사이트 링크


Visual studio 코드 자동 정렬

Ctrl + K + F or Alt + F8

'대학 생활 > C' 카테고리의 다른 글

[C] 유닉스의 open()를 대신하는 메서드  (0) 2014.10.09
[C] 연산자 우선순위  (0) 2014.05.02
[C] error C4996: 'fopen': This function ... 에러  (2) 2014.03.12
[C++]ifstream 클래스  (0) 2013.10.31

+ Recent posts