获取当前目录

kevinhcj 2009-03-17 01:49:08
大家好,

假如目录..\abc\下有 以1~100命名的100个文件夹,在各个文件夹中的文件需要获取当前目录如 ‘1’ 等,我在VS 2005上用如下程序可以实现:

#include "stdio.h"
#include <windows.h> // 这里可能出现问题

int main(int argc, char* argv[])
{
char pwdName[256];
int LEN = 200, LenCurDir;
LenCurDir = GetCurrentDirectory(LEN, pwdName); // 这里可能出现问题
if (LenCurDir == 0)
printf("Failure getting pathname.");
else if (LenCurDir > LEN)
printf("Pathname is too long.");

printf(pwdName);

return 0;
}

但是,这个.cpp文件需要被FLUENT(一个流体力学分析软件)通过UDF调用,编译时出现很多错误。
问题就是加入 #include<windows.h> 头文件引起,FLUENT调用VS进行编译,我已经在机器环境变量里面设置了相应的变量了啊,请大家帮忙分析下可能的问题。
谢谢啊

如下是错误:
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winnt.h(477) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winnt.h(495) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winnt.h(5377) : error C2467: illegal declaration of anonymous 'union'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winnt.h(5394) : error C2467: illegal declaration of anonymous 'union'
...
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\windef.h(331) : error C2371: 'POINT' : redefinition; different basic types
C:\Fluent.Inc\fluent6.3.26\src\profile.h(49) : see declaration of 'POINT'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winbase.h(232) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winbase.h(235) : error C2467: illegal declaration of anonymous 'union'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winbase.h(558) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winbase.h(559) : error C2467: illegal declaration of anonymous 'union'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\winbase.h(2367) : error C2467: illegal declaration of anonymous 'union'
...
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(268) : error C2061: syntax error : identifier 'POINT'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(269) : error C2061: syntax error : identifier 'ptSize'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(269) : error C2059: syntax error : ';'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(272) : error C2059: syntax error : '}'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1112) : error C2061: syntax error : identifier 'POINT'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1114) : error C2059: syntax error : '}'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1893) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1899) : error C2467: illegal declaration of anonymous 'struct'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1900) : error C2467: illegal declaration of anonymous 'union'
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\wingdi.h(1914) : error C2467: illegal declaration of anonymous 'union'
...
...全文
805 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zpple 2009-12-15
  • 打赏
  • 举报
回复
I think it's not Windows problem. What's your computer platform? It seems your code compiles as Alpha platform. If so, I don't know what's happen (I have Intel x86).
Have you VS SP5 on your installation?(转)
kevinhcj 2009-03-18
  • 打赏
  • 举报
回复
to 5 and 8 楼:

方法是不错,但是FLUENT提供的接口中并不包括int main(int argc,char* argv[]),就是说主函数不是自己提供的,似乎没有办法得到argv[]。

还是非常感谢!
yangch_nhcmo 2009-03-17
  • 打赏
  • 举报
回复
写了个不要用到API的

#include <iostream>
#include <cstring>
using namespace std;
const int MaxLen = 256;


int main(int argc,char* argv[])
{

#ifdef _WIN
char ch = '\\';
#else
char ch = '/';
#endif

char PathName[MaxLen];
int Pathlength = strlen(argv[0]);
int cnt = 0;
strcpy(PathName,argv[0]);

printf("%d\t%s\n",Pathlength,PathName);

for (int len = Pathlength - 1; len >= 0 ; len--)
{
if (PathName[len] == ch && cnt < 2)
{
PathName[len] = '\0';
cnt++;
}
if (cnt == 2)
{
printf("%s",&PathName[len+1]);
break;
}
}

return 0;

}

oyljerry 2009-03-17
  • 打赏
  • 举报
回复
文件路径等包含有可能问题,调整检查一下h头文件..
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kevinhcj 的回复:]
就是说自己建立的工程编译运行都是没有问题的,但是FLUENT调用后编译就出现错误了。

其实注释掉 //#include <windows.h>就可以了,但是没有想到其他的办法 GetCurrentDirectory( );
[/Quote]
何解?
Dinelgua 2009-03-17
  • 打赏
  • 举报
回复

std::string strExePath= argv[0];//得到执行程序的绝对路径,然后rfind反向查找到"\"去掉程序名称就是当前目录了
如 strExePath("e:\\test.exe")
kevinhcj 2009-03-17
  • 打赏
  • 举报
回复
to dongpy,

谢谢啊!

但是出的问题还是一样的。如果能不用API就好了。我在研究一下。
dongpy 2009-03-17
  • 打赏
  • 举报
回复
声明一下GetCurrentDirectory函数就可以了。

#include <stdio.h> 
#include <wtypes.h>

WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryA(
DWORD nBufferLength,
LPSTR lpBuffer
);
WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryW(
DWORD nBufferLength,
LPWSTR lpBuffer
);
#ifdef UNICODE
#define GetCurrentDirectory GetCurrentDirectoryW
#else
#define GetCurrentDirectory GetCurrentDirectoryA
#endif

int main(int argc, char* argv[])
{
char pwdName[256];
int LEN = 200, LenCurDir;
LenCurDir = GetCurrentDirectory(LEN, pwdName); // 这里可能出现问题
if (LenCurDir == 0)
printf("Failure getting pathname.");
else if (LenCurDir > LEN)
printf("Pathname is too long.");

printf(pwdName);

return 0;
}
wudeshou82666 2009-03-17
  • 打赏
  • 举报
回复
肯定是要用到API的了!!!
弄清楚怎么用你的fluent调用VS编译吧

kevinhcj 2009-03-17
  • 打赏
  • 举报
回复
就是说自己建立的工程编译运行都是没有问题的,但是FLUENT调用后编译就出现错误了。

其实注释掉 //#include <windows.h>就可以了,但是没有想到其他的办法 GetCurrentDirectory( );

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧