C++中 我想用一个变量作为一个文件的地址,来创建这个文件 请问如何实现

如题所述

第1个回答  2014-02-14
#include<stdio.h>
#include <iostream>
#include <direct.h>
#include <time.h>
using namespace std;
int main()
{
char *buffer; //工程所在路径
if((buffer = getcwd(NULL, 0)) == NULL)
{
perror("getcwd error");
}
else
{
printf("%s\n", buffer);
free(buffer);
}
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );//当前时间

printf ( "\007The current date/time is: %s", asctime (timeinfo) );

/*
以 工程路径 + 当前时间 为文件名创建文件。
*/

getchar();
return 0;
}
第2个回答  2014-02-14
CreateDirectory("D:\\***",NULL);这是创建文件夹的写法。你的问题提的就不清晰。
第3个回答  2014-02-14
你是用什么样的变量来表示文件地址的?文件地址是什么东东?追问

比如我想用程序运行的当前路径和当前的系统时间作为一个文件的名字作为文件的创建路径,来创建这个文件

追答

嗯。
假定这个文件名存在字符串变量fname[]中,则可进行如下操作
FILE *tfp = fopen(fname,"w");
这个文件就被创立了,接下来进行写入操作,完成后,别忘记关闭文件指针,fclose(tfp);。

本回答被提问者采纳
相似回答