无法写在Windows文件 [英] Unable to write files on Windows
问题描述
我想保存使用C一些文件,与此code:
I am trying to save some files using C, with this code:
sprintf(playerinput,"%s",end);
sprintf(fileloc,"%s/.notend",getenv("HOME"));
playerdata = fopen(fileloc, "w+"); /*create the new file*/
if (!playerdata)
{
printf("\n\t\t\tCould not save settings file.");
return;
} else {
fputs(playerinput,playerdata);
fclose(playerdata);
}
应该设置playerinput到底变量,它在Linux下正常工作,然后将文件位置到homearea / .notend,然后进行或编辑文件,并把它拿出来。在Linux中(GCC),这工作正常。同样的code,然而,在Windows(i586的-mingw32msvc-GCC,不工作我做得不对,或者是需要另一个头文件,目前我有:
It should set playerinput to the end variable, which works on Linux, then set the file location to the homearea/.notend, then make or edit the file, and put it out. In Linux (gcc), this works fine. The same code, however, on Windows (i586-mingw32msvc-gcc, does not work. Am I doing something wrong, or is another header file needed? Currently I have:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NOTES 200
#define MAX_NAMES_TEXT 200
#define MAX_NOTES_TEXT 2000
我的头和定义。如果您需要了解更多信息,只问。
as my headers and defines. If you need more information, just ask.
推荐答案
环境变量首页
不是Windows默认的环境变量,以便:
The environment variable HOME
is not a default environment variable on Windows so:
getenv("HOME");
将返回 NULL
。您需要使用不同的功能,以获得在Windows用户的主目录的 的SHGetFolderPath
将提供这样的:
will return NULL
. You need to use a different function to obtain a user's home directory on Windows, SHGetFolderPath
will provide this:
char path[MAX_PATH + 1] = "";
if (SUCCEEDED(SHGetFolderPath(0,
CSIDL_LOCAL_APPDATA,
0,
SHGFP_TYPE_CURRENT,
path)))
{
std::cout << path << "\n";
}
此输出:
C:\\ Documents和Settings \\管理\\本地设置\\应用数据
C:\Documents and Settings\admin\Local Settings\Application Data
我的机器上。
这篇关于无法写在Windows文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!