2014年3月22日18:01:01
创建、重命名文件
1、使用VS2008创建一个带预编译头的控制台项目。
#include "stdafx.h"
#include<iostream>
using namespace std;
int main( void )
{
char ch;
char * strFileName = "a.txt";
FILE * pFileR = fopen(strFileName,"r");
if(NULL == pFileR)//文件不存在。创建文件
{
FILE * pFileW = fopen(strFileName,"w");
if(NULL == pFileW)
{
printf("文件创建失败\n");
cin>>ch;
return 1;
}
else
{
fclose(pFileW);
}
}
else
{
fclose(pFileR);//不要用NULL调用该函数
}
printf("准备重命名文件\n");
cin>>ch;
if(0 !=rename(strFileName,"b.txt"))
{
printf("重命名文件失败\n");
}
cin>>ch;
return 0;
}
2、源文件内容: