在不知道桌面文件路径的情况下,是否能使用C建立一个文件
由于每个人的电脑的桌面路径不一样,有的设置了自己名字,有的是XP有的是WIN7,他们的桌面路径不同,怎么样才能设置成功
#include <stdio.h>
main()
{
FILE *fp;
fp=fopen("C:\\Users\\Lenovo(不一样)\\Desktop\\6.txt","w");
fclose(fp);
}
[解决办法]
用环境变量HOMEPATH,再加上Desktop
[解决办法]
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void){ FILE *fp; int i; char pathname[128]; char userprofile[64]; strcpy(userprofile, getenv("userprofile")); for (i = 0; userprofile[i] != '\0'; i++) { if (userprofile[i] != '\\') pathname[i] = userprofile[i]; else { pathname[i+1] = userprofile[i]; pathname[i] = '\\'; } } pathname[i] = '\0'; strcat(pathname, "\\\\桌面\\\\6.txt"); fp = fopen(pathname, "w"); fclose(fp); return 0;}
[解决办法]
SHGetFolderPath
[解决办法]