I am using getfilepathname() to get the path of a particular file.
but it returns the path of the project. i am using findfirstfile() and
findnextfile() to get the handles, but still it doesnot show the directory
in which handle is present...

can any one help me on this issue ?

Dani AI

Generated

The most likely cause is that the name you get back from FindFirstFile/FindNextFile is just the file name (WIN32_FIND_DATA::cFileName). Calling GetFullPathName on that name will resolve it relative to your process current directory β€” which in an IDE/debug run is often your project folder. was right about the original typo; asked for code; reported the symptom. The fix is to build the full path yourself from the directory you searched plus the returned file name.

Recommended approach (two options):

  • With modern C++ (C++17+): use std::filesystem to turn the search pattern into an absolute path, take its parent directory, and append the found file name.
  • With Win32 APIs: keep the searchPattern you passed to FindFirstFile, strip everything after the last slash to get the directory, make that absolute if it’s relative (GetFullPathName on the directory), then combine directory + cFileName with PathCchCombine or PathCombine.

Example (C++17 std::filesystem):

#include <filesystem>
namespace fs = std::filesystem;

std::string make_full(const std::string &searchPattern, const char *foundName)
{
    fs::path p = fs::absolute(searchPattern); // turns "*.txt" into "C:\current\*.txt"
    p = p.parent_path();                       // remove the wildcard/filename
    return (p / foundName).string();           // combine directory + returned name
}

Example (Win32-style):

char dir[MAX_PATH];
strcpy(dir, searchPattern);
char *s = strrchr(dir, '\\');
if (s) *s = '\0'; else GetCurrentDirectoryA(MAX_PATH, dir);
char full[MAX_PATH];
PathCchCombineA(full, MAX_PATH, dir, fd.cFileName); // requires PathCch library

Notes and cautions: skip "." and ".." entries from FindFirstFile, always call FindClose, prefer PathCch* or std::filesystem for safety, and if you need canonicalization call GetFullPathName on the combined path. See the Win32 docs for FindFirstFile and GetFullPathName for details and edge cases: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew and https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew.

Recommended Answers

All 3 Replies

Have you ever read your own posts?
http://www.daniweb.com/forums/thread149947.html
Well, if you want the answer in this new post: no such function as getfilepathname so you can't get the path of anything from a function which is not in existence.

sorry it is GetFullPathName()...

Plz can u help now....

Remarks

GetFullPathName merges the name of the current drive and directory with a specified file name to determine the full path and file name of a specified file. It also calculates the address of the file name portion of the full path and file name

This function does not verify that the resulting path and file name are valid, or that they see an existing file on the associated volume.

> i am using findfirstfile() and findnextfile() to get the handles
I think you need to show some code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.