0

I need to build a c++ program to monitor a directory for changes the files inside it. The Obtaining Directory Change Notifications is recommended in many questions similar to mine. I tried that code but the first error is

_tsplitpath_s was not declared in this scope

the error belongs to the line

_tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);

I use gcc version 5.1.0 and run the code by the following command in CMD

g++ file.cpp -o out

M a m a D
  • 1,938
  • 2
  • 30
  • 61

2 Answers2

3

The code uses a Microsoft-specific extension to the stdlib.h implementation. _splitpath_s and _wsplitpath_s are only available, when using the stdlib.h implementation that ships with Microsoft's compiler. It is not available in the stdlib.h implementation that comes with GCC.

To work around this, you can either use the Standard C++ filesystem library (may not be immediately available with your compiler), or provide your own implementation of the splitpath functionality.

Alternatively, don't call _tsplitpath_s at all, and make sure to always pass a directory and drive name to WatchDirectory in the sample code.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Thank you. Do you recommend QFileSystemWatcher to do this task? – M a m a D Dec 09 '18 at 10:22
  • @noo: The question is tagged [tag:c++] and [tag:winapi]. `QFileSystemWatcher` is part of Qt. Qt is not part of C++ or the Windows API. If you are fine with using Qt, then go ahead and use what works for you. Personally, I wouldn't recommend using Qt for *anything*, but that's just me. – IInspectable Dec 09 '18 at 10:34
  • I am new to this thing, and I really don't know where to find my solution – M a m a D Dec 09 '18 at 11:10
  • @noo: You already *have* the solution. You just don't know the basics to make it work for you. Head over to [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/1889329), and plan to invest a few years, until you have a solid foundation on which to expand. – IInspectable Dec 09 '18 at 11:17
  • 1
    While your answer is correct for gcc 5.1, it makes it sound as `filesystem` is not in the standard yet. As of C++17 there is [`std::filesystem`](https://en.cppreference.com/w/cpp/filesystem). – zett42 Dec 09 '18 at 12:17
  • I ran that code by Visual Studio 2017 and it gave me no error – M a m a D Dec 09 '18 at 16:22
  • @noo: Yes. Of course. This being a Microsoft-specific extension it is available when using Microsoft's compiler together with Microsoft's support libraries (like *stdlib.h*). – IInspectable Dec 09 '18 at 16:27
  • The `argc` is not equal to `2`. How can I send directory as parameter to `argv`? – M a m a D Dec 09 '18 at 16:36
  • @noo: Get a few books from the list I posted above. They will answer all of those questions. – IInspectable Dec 09 '18 at 16:39
1

To start, make sure you've got <stdlib.h> included.

My psychic powers suggest it's a legacy tchar.h thing. In this day and age, try to avoid using the "t" apis and use the Unicode ones specifically:

If lpDir, lpDrive and other parameters are an ansi strings (char*):

_splitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);

If they are unicode (WCHAR*, wchar_t*, LPCWSTR, etc...):

_wsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);
selbie
  • 100,020
  • 15
  • 103
  • 173
  • 1
    That's not going to help. [_splitpath_s, _wsplitpath_s](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/splitpath-s-wsplitpath-s) are Microsoft-specific extensions, not available in a conforming *stdlib.h* implementation other than Microsoft's. The clue here is the leading underscore (`_`), moving the symbol into the namespace reserved for use by the implementation. – IInspectable Dec 09 '18 at 09:05
  • True. Avoiding the `TCHAR`-based functions remains as a good advise though! – Ulrich Eckhardt Dec 09 '18 at 09:30