Answered by:
stat() fails if path has a trailing backslash

Question
-
Hi!
I have a directory "C:\stageroot".
If I pass filename to stat() as follows, stat() returns non-zero exit code.
filename = "C:\\stageroot" stat(filename, &sb);
However if filename has a trailing backslash, stat() fails with ENOENT.
filename = "C:\\stageroot\\" stat(filename, &sb);
Is this expected? I am using VS2008.
Thanks!
- Moved by CoolDadTx Friday, August 7, 2020 2:13 PM C++
Friday, August 7, 2020 7:00 AM
Answers
-
You posted in C# forum...
A test in C++, VS 2015, on Windows 10 works for me, nRet = 0 =>
char sFilename[MAX_PATH] = "C:\\temp\\"; struct stat buf; int nRet = stat(sFilename, &buf);
- Marked as answer by nsp2092 Wednesday, August 12, 2020 6:35 AM
Friday, August 7, 2020 11:08 AM
All replies
-
Hi nsp2092,
Thank you for posting here.
Theoretically speaking, backslashes cannot be added to directory names in windows.
Did I misunderstand what you mean?
In any case, try the following method to see if it works.
string fileName = @"filename";
Or use Path.Combine Method.
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Timon YangMicrosoft contingent staff Friday, August 7, 2020 8:37 AM
Friday, August 7, 2020 8:36 AM -
Thank you!
I am reading the filepath value from a configuration file.
In the config file:
$filepath C:\stageroot\
The directory name is stageroot (so it has so \ in the name), but while providing the path I gave the absolute path "C:\stageroot\". This fails in stat().
If the config file is:
$filepath C:\stageroot
then stat() is able to find the stageroot directory.
The above code is in C, not Cpp.
Thanks!
Friday, August 7, 2020 10:52 AM -
You posted in C# forum...
A test in C++, VS 2015, on Windows 10 works for me, nRet = 0 =>
char sFilename[MAX_PATH] = "C:\\temp\\"; struct stat buf; int nRet = stat(sFilename, &buf);
- Marked as answer by nsp2092 Wednesday, August 12, 2020 6:35 AM
Friday, August 7, 2020 11:08 AM -
I have a directory "C:\stageroot".
If I pass filename to stat() as follows, stat() returns non-zero exit code.
filename = "C:\\stageroot" stat(filename, &sb);
However if filename has a trailing backslash, stat() fails with ENOENT.
filename = "C:\\stageroot\\" stat(filename, &sb);
Is this expected? I am using VS2008.
Do you mean that it fails in both cases? Which return code and errno do you have in first code?Friday, August 7, 2020 12:38 PM -
Sorry, new to the forum.
You are right. It works in VS 2017. So this may be a VS 2008 specific issue.
Friday, August 7, 2020 5:20 PM -
No. It fails only in case 2 i.e. when filename contains a trailing backslash. The errno in Case 2 is ENOENT. This is happening only in VS 2008.Friday, August 7, 2020 5:22 PM
-
Hi,
Thank you for posting here.
>>The errno in Case 2 is ENOENT. This is happening only in VS 2008.
A return value of -1 indicates an error, in which case errno is set to ENOENT, indicating that the filename or path could not be found.
As far as I'm concerned, it seems an issue in vs2008. I suggest you could post the issue to the Developer Community for better help.
And I suggest you could try to use the a newer version of visual studio: https://visualstudio.microsoft.com/zh-hans/downloads/
Best Regards,
Jeanine Zhang
"Visual c++" forum will be migrating to a new home on Microsoft Q&A !
We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A !
For more information, please refer to the sticky post.Monday, August 10, 2020 2:19 AM