Read file at startup

ArielVW 1 Reputation point
2022-10-22T16:08:18.553+00:00

I made an application to read files and the application runs normally. But the problem is when the computer is restarted this application can't read the file (I've entered the application path to registry HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run manually).

#define WIN32_LEAN_AND_MEAN   
#include <stdio.h>  
#include <windows.h>  
  
int main(void ) {  
   TCHAR config[MAX_PATH];  
   GetCurrentDirectory(MAX_PATH,config);  
   strcat(config,"\\test.txt");  
  
   FILE* fPtr = fopen(config, "r");  
    if (fPtr == NULL){  
        fclose(fPtr);  
        printf("File not exists");  
    } else {  
        fclose(fPtr);  
        printf("File exists");  
    }  
  
    getchar();  
  
   return 0;  
}  
Windows development | Windows API - Win32
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-10-22T16:38:49.137+00:00

    Don't use GetCurrentDirectory
    Use GetModuleFileName or other...

    0 comments No comments

  2. RLWA32 49,636 Reputation points
    2022-10-22T16:42:48.197+00:00

    Use the fully qualified path to the file in your code. The current directory when the user logs on is probably not the same as when you run your tests.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.