ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파일 존재여부 확인 (faile )
    카테고리 없음 2013. 3. 16. 00:33
    반응형

    int _tmain(int argc, _TCHAR* argv[])

    {


    ifstream input;//파일 객체 생성

    input.open("test.txt");//파일 열기

    //fail 은 파일이 없으면 true 반환,있으면 false 를 반환 

    //--> 파일존재여부를 확인한 다음 작업을 수행하면 파일 생성시 발생한 오류 방지함

    if (input.fail())

    {

    cout<<"파일이 존재 하지 않는다."<<endl;

    return 0;

    }

    else

    {

    cout<<"파일 열기후 실행"<<endl;

    input.close();

    }

    return 0;

    }


    =======================================

    int _tmain(int argc, _TCHAR* argv[])

    {


    ofstream output;//출력할 파일 객체 생성

    char cont = 'y';

    char word[50]={0,};

    //출력파일 열기 

    output.open("test.txt");


    if (output.fail())

    {

    return 1;

    }

    while (cont =='y' || cont =='Y')

    {

    cout<<"단어 입력 :";

    cin>>word;//표준 입력 장치

    output<<word<<endl;//출력파일에 내용 출력

    cout<<"계속 ? (y/n) ";

    cin>>cont;

    }

    output.close();//닫기

    return 0;

    }

    반응형
Designed by Tistory.