настоящее время я использую Code :: Blocks 13,12 (составитель: GNU GCC) под ОС Windows 10.C Еореп() - Возможная проблема с абсолютным путем
Я пытаюсь открыть файл и загрузить его содержимое, но fopen дает мне неприятности. «Input.txt» существует в том же каталоге, что и мой исполняемый файл. Я уже проверял разрешения.
Функция для получения пути:
char* getFileName()
{
char *fileName; /* the path of the .txt file */
char path[MAX_PATH];
/* get the path of the executable */
GetModuleFileName(NULL, path, MAX_PATH);
/* remove the name of the executable from the path */
PathRemoveFileSpec(path);
/* check case where path is directory root */
if(PathIsRoot(path))
strcat(path, "\\*");
/* add the name of the .txt file to the path */
strcat(path, "\\input.txt");
/* not sure if needed */
path[strlen(path)] = '\0';
fileName = strdup((char *) path);
return fileName;
}
Функция для загрузки содержимого файла:
bool loadDict(char *fileName)
{
FILE *fp; /* file stream */
char line[LINE_SIZE]; /* each line of the file */
// other variables
/* try to open file for reading */
if((fp = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Error: Could not open the file '%s' to read\n", fileName);
return false;
}
// stuff done
/* file is no longer needed, close it */
if(fclose(fp))
{
fprintf(stderr, "Error: Could not close the file '%s' to read\n", fileName);
return false;
}
return true; /* in case no problem has occured */
}
Main:
int main()
{
char *fileName;
fileName = getFileName();
/* try to load the dictionary into memory */
if(!loadDict(fileName))
{
fprintf(stderr, "Error: The dictionary could be not loaded into memory.\nProgram terminating...\n");
return 1;
}
// other stuff
return 0;
}
Я получаю и ошибки (может не открывать файл, не удалось загрузить). Я уже пытался заменить '\' на '/' или использовать двойные слэши без успеха.
FILE *fp = fopen("path\\input.txt", "r");
Любая помощь будет принята с благодарностью.
Ваш путь содержит пробелы или символы, отличные от ASCII? Это может быть проблемой –
Нет, путь состоит только из латинских букв. –
Прекратить использование инструментов, которые по умолчанию соответствуют кодировке MBCS (ANSI). Для справки: Это 2016 год. – IInspectable