当前位置: 代码迷 >> C语言 >> [求助]fopen函数的问题
  详细解决方案

[求助]fopen函数的问题

热度:271   发布时间:2007-10-05 18:13:07.0
[求助]fopen函数的问题
我想建立一个文件在 system32下,但是XP和,Windows 2000的 系统目录不一样,
所以我想建立一个判断

File*cyc

cyc=fopen("c:\\windows\\system32\\cyc.exe","w");//但是这里如果安装在Windows2000上,一定有一个无法创建的错误,返回一个数值,请问是0还是1还是别的什么?

if(cyc=0)fopen("c\\winnt\system32\\cyc.exe","w");

不知道这样对么??
请高手指教!
搜索更多相关的解决方案: fopen  函数  

----------------解决方案--------------------------------------------------------

我知道,调系统API,有个叫GetSystemDirectory的函数
具体用法如下
GetSystemDirectory
The GetSystemDirectory function retrieves the path of the system directory. The system directory contains such files as dynamic-link libraries, drivers, and font files.

UINT GetSystemDirectory(
LPTSTR lpBuffer, // buffer for system directory
UINT uSize // size of directory buffer
);
Parameters
lpBuffer
[out] Pointer to the buffer to receive the null-terminated string containing the path. This path does not end with a backslash unless the system directory is the root directory. For example, if the system directory is named WINDOWS\SYSTEM on drive C, the path of the system directory retrieved by this function is C:\WINDOWS\SYSTEM.
uSize
[in] Specifies the maximum size of the buffer, in TCHARs. This value should be set to at least MAX_PATH.
Return Values
If the function succeeds, the return value is the length, in TCHARs, of the string copied to the buffer, not including the terminating null character. If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
Applications should not create files in the system directory. If the user is running a shared version of the operating system, the application does not have write access to the system directory. Applications should create files only in the directory returned by the GetWindowsDirectory function.

For an example, see Getting System Information.

Requirements
Windows NT/2000 or later: Requires Windows NT 3.1 or later.
Windows 95/98/Me: Requires Windows 95 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.


----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <process.h>
main()
{
char * GetWinSysPath() ;
{
#define MAX_PATH_LEN 256
AnsiString lpBuffer;
lpBuffer.SetLength(MAX_PATH_LEN);
GetSystemDirectory(lpBuffer.c_str(),MAX_PATH_LEN);
int pos=lpBuffer.Pos("System");
AnsiString path=lpBuffer.SubString(1,pos-1);
return path.c_str();
}
}
为什么无法编译啊
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <process.h>
main()
{
char * GetWinSysPath() ;
{
#define MAX_PATH_LEN 256
AnsiString lpBuffer;
lpBuffer.SetLength(MAX_PATH_LEN);
GetSystemDirectory(lpBuffer.c_str(),MAX_PATH_LEN);
int pos=lpBuffer.Pos("System");
AnsiString path=lpBuffer.SubString(1,pos-1);
return path.c_str();
}
}
为什么无法编译啊
----------------解决方案--------------------------------------------------------
因为你用了API。。。

在C下用API我不在行,头文件一般是windows.h,不过具体你要查一查。。

在你原有思路上,只需:

cyc=fopen(...)
if(cyc==NULL) 不存在
else{
存在
fclose(cyc);
}
----------------解决方案--------------------------------------------------------
是啊,windows.h我也用过,不过 AnsiString lpBuffer; 找不到头文件不知道是那个

不过API确实想学学。 请问哪里能找到质料呢
不过楼上的朋友,十分感谢,我就按照这个思路接着写了。
有机会在学学API吧。。。
----------------解决方案--------------------------------------------------------
  相关解决方案