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);
}
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.
The environment variable
HOMEis not a default environment variable on Windows so:will return
NULL. You need to use a different function to obtain a user’s home directory on Windows,SHGetFolderPathwill provide this:This output:
on my machine.