我很急啊,帮帮我

付新军 2015-01-07 02:16:29

#include "stdio.h"
//#include "conio.h"
#include "direct.h"
int main()
{
char str[50];
int i=250;
sprintf(str,"%d",i);
printf("%s\n",str);//我要根据i的值动态创建文件夹,文件夹的名字也动态生成
int status;
status=0;
status=mkdir("./mydir/str");//但是这里创建的文件夹名字是“str”
printf("status=%d",status);
//getch();
return 1;//要如何修改才能达到我的要求???谢谢了,我很急!
}
...全文
302 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2015-01-15
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
[quote=引用 12 楼 lm_whales 的回复:] 子目录要逐级创建,除非确定该目录的上级目录存在
MakeSureDirectoryPathExists The MakeSureDirectoryPathExists function creates all the directories in the specified DirPath, beginning with the root. BOOL MakeSureDirectoryPathExists( IN LPSTR DirPath ); Parameters DirPath Pointer to a null-terminated string that specifies a valid path name. If the final component of the path is a directory, not a filename, the string must end with a backslash (\) character. Return Values If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To retrieve extended error information, call GetLastError. Remarks Each directory specified is created, if it does not already exist. If only some of the directories are created, the function will return FALSE. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Available as a redistributable for Windows 95. Windows CE: Unsupported. Header: Declared in imagehlp.h. Import Library: Use imagehlp.lib. See Also PE Image Helper (ImageHlp) Overview, ImageHlp General Functions [/quote]这个API 好,不过他只是 Windows 的API 函数, mkdir 本来是C库函数(编译系统自带的),不知道为何没有成为标准库函数
lm_whales 2015-01-13
  • 打赏
  • 举报
回复
子目录要逐级创建,除非确定该目录的上级目录存在
lm_whales 2015-01-13
  • 打赏
  • 举报
回复
#include<cstdio> #include<cstring> #include<sys/stat.h> using namespace std; int createDir(char * name){ if(access(name,0)==-1){ return mkdir(name); } return -1; } int createDir(char * path,int no) { char pathname[MAX_PATH]; sprintf(pathname,"%s\\%s\\%d",no); return createDir(pathname); } int main(int agrv, char ** argc) { char str[50]; createDir(".\\mydir"); for(int i = 0; i < 20; ++i) { createDir(".\\mydir",i); } return 0; }
赵4老师 2015-01-13
  • 打赏
  • 举报
回复
引用 12 楼 lm_whales 的回复:
子目录要逐级创建,除非确定该目录的上级目录存在
MakeSureDirectoryPathExists The MakeSureDirectoryPathExists function creates all the directories in the specified DirPath, beginning with the root. BOOL MakeSureDirectoryPathExists( IN LPSTR DirPath ); Parameters DirPath Pointer to a null-terminated string that specifies a valid path name. If the final component of the path is a directory, not a filename, the string must end with a backslash (\) character. Return Values If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To retrieve extended error information, call GetLastError. Remarks Each directory specified is created, if it does not already exist. If only some of the directories are created, the function will return FALSE. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Available as a redistributable for Windows 95. Windows CE: Unsupported. Header: Declared in imagehlp.h. Import Library: Use imagehlp.lib. See Also PE Image Helper (ImageHlp) Overview, ImageHlp General Functions
付新军 2015-01-10
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
sprintf(str,"D:\\mydir%d",i);
我用这个斜杠/ 也可以啊 vc6.0
此后三年 2015-01-08
  • 打赏
  • 举报
回复
Falleyes 2015-01-08
  • 打赏
  • 举报
回复
Linux和Windows下地址里的斜杠\'和'/'是相反的。
li4c 2015-01-08
  • 打赏
  • 举报
回复
#include<cstdio>
#include<cstring>
#include<sys/stat.h>
using namespace std;

int main(int agrv, char ** argc)
{
	char str[50];

	for(int i = 0; i < 20; ++i)
	{
		sprintf(str,"./hello%d",i);
		mkdir(str,0755);
	}
         return 0;
}
707wk 2015-01-08
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
sprintf(str,"D:\\mydir%d",i);
赵4老师 2015-01-07
  • 打赏
  • 举报
回复
sprintf(str,"D:\\mydir%d",i);
动力风暴 2015-01-07
  • 打赏
  • 举报
回复
请教楼主为啥要写成这样:
sprintf(str,"./mydir/%d",i);
是在linux环境下吗???
我在vs2010下试,这样不能正确创建目录:

写成这样
sprintf(str,"D:\mydir%d",i);

或这样
sprintf(str,"mydir%d",i);
才能创建目录
jacksonfan 2015-01-07
  • 打赏
  • 举报
回复
有问题就调试跟踪
版主大哥 2015-01-07
  • 打赏
  • 举报
回复
付新军 2015-01-07
  • 打赏
  • 举报
回复
#include "stdio.h"
//#include "conio.h"
#include "direct.h"
int main()
{
	char str[50];
	int i=250;
	sprintf(str,"./mydir/%d",i);
	printf("%s\n",str);
	int status;
	status=0;
	status=mkdir(str);
	printf("status=%d",status);
	//getch();
	return 1;//自己整好了,看来太急了不行,急了就想不出办法
}

69,377

社区成员

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

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