第2个回答 2010-09-12
#include "windows.h"
int _tmain(int argc, TCHAR *argv[])
{
char pBuf[MAX_PATH];
char pBufForSave[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH,pBuf); //获取程序的当前目录
strcpy_s(pBufForSave,MAX_PATH,pBuf);
strcat_s(pBuf,MAX_PATH,"\\path.txt");
HANDLE hFile =CreateFileA(pBuf,
GENERIC_WRITE,
NULL,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return -1;
}
DWORD dwSize = strlen(pBufForSave);
WriteFile(hFile,pBufForSave,dwSize,&dwSize,NULL);
CloseHandle(hFile);
return 1;
}
第3个回答 2010-09-12
程序如下:
#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[1000];
GetCurrentDirectory(1000,buf); //得到当前工作路径
cout<<buf<<endl;
fstream file("path.txt",ios::out);
if(!file)
cerr<<"open fail"<<endl;
file<<buf;
file.close();
}
有问题hi我,交流下~
还有个获取路径的方法:
#include <stdio.h>
#include <direct.h>
#define MAXPATH 1000
int main(void)
{
char buffer[MAXPATH];
getcwd(buffer, MAXPATH);
printf("The current directory is: %s\n", buffer);
return 0;
}