| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1033 人关注过本帖
标题:[求助]fopen函数的问题
只看楼主 加入收藏
cycchina
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-8-24
收藏
 问题点数:0 回复次数:5 
[求助]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 函数 
2007-10-05 18:13
sccdyc
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-4-23
收藏
得分:0 

我知道,调系统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.


2007-10-05 18:33
cycchina
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-8-24
收藏
得分:0 
#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();
}
}
为什么无法编译啊

================================= 老师们好! =================================
2007-10-05 23:21
cycchina
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-8-24
收藏
得分:0 
#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();
}
}
为什么无法编译啊

================================= 老师们好! =================================
2007-10-05 23:27
coachard
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:1251
专家分:0
注 册:2007-8-12
收藏
得分:0 
因为你用了API。。。

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

在你原有思路上,只需:

cyc=fopen(...)
if(cyc==NULL) 不存在
else{
存在
fclose(cyc);
}

偶学编程,也许本身就是一个错。。。
2007-10-05 23:57
cycchina
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-8-24
收藏
得分:0 
是啊,windows.h我也用过,不过 AnsiString lpBuffer; 找不到头文件不知道是那个

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

================================= 老师们好! =================================
2007-10-06 08:03
快速回复:[求助]fopen函数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019174 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved