-1

Im begginer to C, currently have to learn C and Win32 API, and in my first project i need to get from the user a path name and a file name, then check if the file exists and delete it if the user wants to.

Im currently stuck at finding whether a file exists. Im familiar with a solution that was shown in this site before (What's the best way to check if a file exists in C?) but I have been hinted/instructed to use a function called getfullpathname() in order to parse the strings and then checking if the file entered exists. My problem is that all GetFullPathName does as far as i searched (tried to understand the MSDN and couple or more sites) is concatinating the working drive and directory onto the filename you've provided. Am i missing something? Do i need to change the working directory to the path entered in order to concatinate the path and the name file or just pass to the function the path for it to parse it so i could be able to do the checking? Do i need this function only for parsing the path or to concatinate the path string and the name string? Could you provide me with the example of doing this first part of the project?

Thanks in advance.

StepSis
  • 31
  • 1
  • 3
  • having to follow _i need to get from the user a path name and a file name_ and _I have been hinted/instructed to use a function called getfullpathname()_ you are right to say _i need to change the working directory to the path entered ..._ even this is complicated for nothing (because of the rules you have to follow) ^^ – bruno Apr 01 '19 at 17:44
  • 1
    for check are file exist use say `GetFileAttributes`, but in most case - not need check at all. want delete file - just do this. if file not exist - you get error which say this. – RbMm Apr 01 '19 at 17:45
  • @RbMm I know, but the use of this function is for the purpose of the excersice, it focuses on the winapi more than the C. – StepSis Apr 01 '19 at 18:36
  • so use `GetFileAttributes` for check are file exist – RbMm Apr 01 '19 at 18:57
  • Could the idea of the exercise be that **you** are supposed to write `GetFullPathName()` ? – Yunnosch Apr 02 '19 at 05:22

2 Answers2

1

concatinating the working drive and directory onto the filename you've provided.

Not a simple concatenating, This function does not check if the file exists, but just parse the relative path of the file(no matter whether the file exists) to the absolute path. The first parameter of Function GetFullPathName is relative path of the file you need to put in. If the file is located under the current working directory, you only need to send the filename to the function call. If the file is located in the upper path, then you can send ../filename, the function will parse it to an absolute file path.

You could use GetShortPathName. If the file does not exist, the call will fail, and return 0.

oWWo
  • 81
  • 5
0

Hmmm, According to my practice

Assuming that the file is in the current working directory, GetFullPathName sounds like a good idea. It accepts a file name and converts it to a full path by presetting the current working directory.

Note: The API returns a path regardless of whether the file exists in the working directory or not; it only uses the file name you provide and prepares the current working directory in advance.

DEMO1:

#include <windows.h>

int main()
{
   char filename[] = "test.txt";
   char fullFilename[MAX_PATH];
   GetFullPathName(filename, MAX_PATH, fullFilename, nullptr);
   MessageBox(NULL, fullFilename, "DEBUG", MB_OK);
}

Debug Result:

Messagebox

In fact, there is no test. txt text document at all.

So you can do it in the following way

DEMO2:

#include <windows.h>

int main()
{
   char lpszPath[] = "..\\Project20";
   long     length = 0;
   TCHAR*   buffer = NULL;

   buffer = new TCHAR[length];
   length = GetShortPathName(lpszPath, buffer, length);

   if (length == 0)
   {
     MessageBox(NULL, "ERROR", "DEBUG", MB_OK);
   }
   else
   {
     MessageBox(NULL, "SUCCESS", "DEBUG", MB_OK);
   }
   delete[] buffer;

   return 0;
}

Judgment of the existence of documents through ERROR and SUCCESS file loaction

SUCCESS

Strive Sun
  • 5,988
  • 1
  • 9
  • 26