判断文件是否存在

    //当文件存在时,加(1),(2)...  added by Qian 2012/7/31

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    std::string szFileName = strFullPath.substr(strFullPath.rfind("\\") + 1); //最原始的无路径文件名
    int i = 1;//当前重复次数
    while( (hFind = ::FindFirstFile((LPCSTR)strFullPath.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
    {  
     char buf[4];
     sprintf(buf,"(%d)",i);     
     std::string strNewFileName = szFileName;//调整后的文件名
     strNewFileName.insert(strNewFileName.find_last_of("."),buf);
     //删除原路径中的文件名
     strFullPath.erase(strFullPath.find_last_of("\\"), strFullPath.size());
     //将调整后的文件名加入路径
     strFullPath.append("\\").append(strNewFileName);
     FindClose(hFind);
     i++;
    }

程序调试

 

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <fstream>
#include <string>
#include <io.h>
#include   <Windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 std::string str = "C:\\Users\\Administrator\\AppData\\Local\\JtangIM\\用户名\\FileRecv\ //当文件存在时,加(1)  added by Qian 2012/7/31

 FILE *stream;
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind;

 while( (hFind = FindFirstFile((LPCWSTR)str.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
 {
  int i = 1;
  char buf[4];
  sprintf(buf,"(%d)",i); 
  str.insert(str.find_last_of("."),buf);
  FindClose(hFind);
  i++;
 }

//  int t = dd.find_last_of(".");
//     FILE *stream;
//
//  WIN32_FIND_DATA FindFileData;
//  HANDLE hFind;
//
//  while( (hFind = FindFirstFile((LPCWSTR)str.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
//  {
//   int last = str.find_last_of( "\\" );
//   str.erase(last, str.size());
//   int i = 1;
//   char buf[4];
//   sprintf(buf,"(%d)",i);
//   dd.insert(t,buf);
//   str.append(dd);
//   FindClose(hFind);
//  }
 //   while ( (stream  = fopen( "data", "r" )) == NULL )
//  while( !::CreateDirectory((LPCWSTR)str.c_str(), NULL) )
//  {
//   last = str.find_last_of( "\\" );
//   str.erase(last, str.size());
//   int i = 1;
//   char buf[4];
//   sprintf(buf,"(%d)",i);
//   dd.insert(t,buf);
//   str.append(dd);
//  }
//  while ( i != -1 )
//   int last = str.find_last_of( "\\" );
 return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值