CFileStatus fs;
if(! CFile::GetStatus(_T("C:\\winnt\\output.txt") , fs) )
{
  AfxMessageBox(_T("file not exists!");
}

解决方案 »

  1.   

    将文件试着打开也能做到,如:
    ifstream fp;
    fp.open("c:\\winnt\\output.txt",ios::nocreate);
    if(fp==NULL)
      AfxMessageBox("file not exists");
    fp.close();
      

  2.   

    借你的代码改一下:
    HANDLE hFile = CreateFile(_T("C:\\winnt\\output.txt"),
                        GENERIC_WRITE, NULL,
                    NULL, CREATE_NEW, NULL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    AfxMessageBox(_T("Couldn't create the file!")
      

  3.   

    那你就用ANSI C吧,VC 也支持ANSI C 
    你用
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <error.h>
    struct stat st_buff;
    int iret = stat("C:\\winnt\\output.txt",&st_buff );
    if( iret==-1 ){
      if( errno = ENOENT ){
        AfxMessageBox(_T("文件已经存在!");
        return ;
      }
    }else{
    HANDLE hFile = CreateFile(_T("C:\\winnt\\output.txt"),
                        GENERIC_WRITE, FILE_SHARE_READ,
                    NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
     if (hFile == INVALID_HANDLE_VALUE){
       AfxMessageBox(_T("创建文件错");
       return;
     }
    }
    这样应该可以ok。
      

  4.   

    错了,改一下
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    struct stat st_buff;
    int iret = stat("C:\\winnt\\output.txt",&st_buff );
    if( iret!=-1 ){
      AfxMessageBox(_T("文件已经存在!");
      return ;
    }else{
    HANDLE hFile = CreateFile(_T("C:\\winnt\\output.txt"),
                        GENERIC_WRITE, FILE_SHARE_READ,
                    NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
     if (hFile == INVALID_HANDLE_VALUE){
       AfxMessageBox(_T("创建文件错");
       return;
     }
    }
      

  5.   

    用GetFileAttributes就可以那
      

  6.   

    PathFileExists
    Determines if a file exists. BOOL PathFileExists(
        LPCTSTR pszPath
        );Parameters
    pszPath 
    Address of the file to verify. 
    Return Values
    Returns TRUE if the file exists, or FALSE otherwise.Example
    #include <windows.h>
    #include <iostream.h>
    #include "Shlwapi.h"void main( void )
    {
    // Valid file path name (file is there).
    char buffer_1[] = "C:\\TEST\\file.txt"; 
    char *lpStr1;
    lpStr1 = buffer_1;// Invalid file path name (file is not there).
    char buffer_2[] = "C:\\TEST\\file.doc"; 
    char *lpStr2;
    lpStr2 = buffer_2;
    // Return value from "PathFileExists".
    intretval;// Search for the presence of a file with a true result.
    retval = PathFileExists(lpStr1);
    if(retval == 1)
    {
    cout << "Search for the file path of : " << lpStr1 << endl;
    cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
    cout << "The return from function is : " << retval << endl;
    }else{
    cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
    cout << "The return from function is : " << retval << endl;
    }// Search for the presence of a file with a false result.
    retval = PathFileExists(lpStr2);
    if(retval == 1)
    {
    cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
    cout << "Search for the file path of : " << lpStr2 << endl;
    cout << "The return from function is : " << retval << endl;
    }else{
    cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
    cout << "The return from function is : " << retval << endl;
    }
    }
    OUTPUT
    ==============
    Search for the file path of : C:\TEST\file.txt
    The file requested "C:\TEST\file.txt" is a valid file
    The return from function is : 1The file requested "C:\TEST\file.doc" is not a valid file
    The return from function is : 0Res
    This function tests the validity of the file and path. It works only on the local file system or on a remote drive that has been mounted to a drive letter. It will return FALSE for remote file paths that begin with the UNC names \\server or \\server\share. It will also return FALSE if a mounted remote drive is out of service. Requirements 
      Version 4.71 and later of Shlwapi.dll  Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later). 
      Windows 95/98: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later). 
      Header: Declared in shlwapi.h. 
      Import Library: shlwapi.lib.
      

  7.   

    会不会是在文件前头没加上#include "fstream.h"这条语句吧
      

  8.   

    抱歉,龙江老三,你的程序还是错,报#include <unistd.h>没有unistd.h文件的错。
    大章鱼兄,后来加上哪个头文件后,成功了,谢谢!!!
      

  9.   

    龙江老三,抱歉,程序错误,报#include <unistd.h>的错,说没有unistd文件
    对了,大章鱼兄,加上那个头文件后,通过了,谢谢!!!!
      

  10.   

    对了,章鱼兄ifstream和 ios::nocreate表示什么,是个什么概念,可否简单说明一下!!!
    我看联机帮助,就是没有找到ifstream的函数,更不要说是open函数了!!!
      

  11.   

    ifstream是一个输入流,是从iostream类派生的,iostream类是C++所特有的,即"流".而open是ifstream的成员函数,ios::nocreate是一个open打开文件标志的一种,是判断文件是否存在,表示如果文件不存在,则打开失败.你要是对流还不太熟悉的话,要找C++的书看看,流可是C++的一大特性,用熟了会很好用的.