读书人

怎么用C语言在硬盘上创建个文件夹?高手

发布时间: 2012-03-06 20:47:55 作者: rapoo

如何用C语言在硬盘上创建个文件夹??高手请讲解下
在C 中用fp=fopen( "filename.000 ", "w+ ") 可以创建一个不存在文件名为filename.000的文件.
若用fp=fopen( "file\\name.000 ", "w+ ") 如果file文夹件价不存在的话则不会创建文件夹file而是fp=NULL;
如何创建一个文件夹?高手请讲解下 谢谢了~~~

[解决办法]
mkdir是建文件夹的命令,folder是文件夹名。
[解决办法]
或者使用这个函数:
函数名称: mkdir
函数原型: int mkdir(const char *path)
函数功能: 建立名称由path指定的目录
函数返回: 0:操作成功,-1:操作失败
参数说明: path-要建立的目录名称
所属文件: <dir.h>

#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dir.h>
int main()
{
int status;
clrscr();
status=mkdir( "asdfjklm ");
(!status)?(printf( "Directory created ")):(printf( "Unable to create directory "));
getch();
system( "dir ");
getch();
status=rmdir( "asdfjklm ");
(!status)?(printf( "Directory deleted ")):(perror( "Unable to delete directory "));
return 0;
}
[解决办法]
也可以用API函数

#include <windows.h>

BOOL CreateDirectory(
LPCTSTR lpPathName, // pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor
);

Parameters
lpPathName
Pointer to a null-terminated string that specifies the path of the directory to be created.
There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the CreateDirectory function parses paths.

eg:

char pathname[MAX_PATH];
//给pathname赋值
CreateDirectory(pathname,NULL);//如果不考虑文件的安全性,置为NULL

读书人网 >C语言

热点推荐