请教各位大神一个问题~

huyijinai 2013-07-05 04:21:31
#include <io.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n=0;
char dir[12];

while(1)
{
n++;
sprintf(dir,"md %02d >nul",n);
if(system(dir)==0)
break;
}
return 0;
}
这个小程序,第一次运行是生成一个名为01的文件夹,第二次运行生成02,以此类推。
想请教下各位高手这个程序运行的原理,还有
if(system(dir)==0)
break;的作用~
...全文
332 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
谢谢啦,做出来了,非常感谢你的耐心解答~
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
我写的代码是在存在A1到A30的基础上,创建01 然后移动A1至A5到01中,创建02 然后移动A6至A10到02中,直到06
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
我刚才调了下,是可以实现你要的功能的,只是char dir长度小了点,你把我的代码里面

char dir[12];

char dir[15];
就不会出错了,记得你在运行前要删除所有的01 02 03这样的目录,然后运行目录下面必须要存在A1 A2一直到A30。这样就行了
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
我试了一下,有点错误,抱歉我因为什么都还不懂,只能看懂一部分代码,希望你能帮我写个完整的,谢谢你~
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
如果是50个a开头的文件的话把i小于5改成i小于50就ok
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
引用 15 楼 huyijinai 的回复:
大神,问题问错了。是这样的,现在外部有许多个以A开头的文件夹,之后将50个以A开头的文件夹放入程序生成的01文件夹中,以此类推,就是每生成一个文件夹,就往里面放50个以A开头的文件夹,这50个以A开头的文件夹是已经有的了。大体就是这样,我都晕了~
这个简单,我刚才那个代码改一下就可以了

#include <io.h>
 #include <stdio.h>
 #include <stdlib.h>    
 int main() {     
 int n=0;     
 char dir[12];     
 int m = 1;      
 while(1)     
 {         
 n++;         
 sprintf(dir,"md %02d >nul",n);         
 if(system(dir)==1)             
 break;         
 for(int i=0;i<5;i++ )                   
 {                        
 sprintf(dir,"move A%d %02d ",m,n);                        
 m++;                         
 if(system(dir)==1)                         
 break;                     
 }     
 if(n>5)///////////////
 break;
 }     
 return 0; 
 } 
你试一下这个代码,我没调不过思路就是这样的,其实就是改一下生成的命令而已
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
大神,问题问错了。是这样的,现在外部有许多个以A开头的文件夹,之后将50个以A开头的文件夹放入程序生成的01文件夹中,以此类推,就是每生成一个文件夹,就往里面放50个以A开头的文件夹,这50个以A开头的文件夹是已经有的了。大体就是这样,我都晕了~
赵4老师 2013-07-05
  • 打赏
  • 举报
回复
system, _wsystem Execute a command. int system( const char *command ); int _wsystem( const wchar_t *command ); Routine Required Header Compatibility system <process.h> or <stdlib.h> ANSI, Win 95, Win NT _wsystem <process.h> or <stdlib.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value If command is NULL and the command interpreter is found, the function returns a nonzero value. If the command interpreter is not found, it returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values: E2BIG Argument list (which is system-dependent) is too big. ENOENT Command interpreter cannot be found. ENOEXEC Command-interpreter file has invalid format and is not executable. ENOMEM Not enough memory is available to execute command; or available memory has been corrupted; or invalid block exists, indicating that process making call was not allocated properly. Parameter command Command to be executed Remarks The system function passes command to the command interpreter, which executes the string as an operating-system command. system refers to the COMSPEC and PATH environment variables that locate the command-interpreter file (the file named CMD.EXE in Windows NT). If command is NULL, the function simply checks to see whether the command interpreter exists. You must explicitly flush (using fflush or _flushall) or close any stream before calling system. _wsystem is a wide-character version of system; the command argument to _wsystem is a wide-character string. These functions behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tsystem system system _wsystem Example /* SYSTEM.C: This program uses * system to TYPE its source file. */ #include <process.h> void main( void ) { system( "type system.c" ); } Output /* SYSTEM.C: This program uses * system to TYPE its source file. */ #include <process.h> void main( void ) { system( "type system.c" ); } Process and Environment Control Routines See Also _exec Functions, exit, _flushall, _spawn Functions
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
还有记得你每次运行程序前要把之前调试时候生成的文件删掉,不然因为文件已经存在了,system函数会返回创建失败的信息,为了方便调试最好再加一句话 #include <io.h> #include <stdio.h> #include <stdlib.h> int main() { int n=0; char dir[12]; int m = 1; while(1) { n++; sprintf(dir,"md %02d >nul",n); if(system(dir)==1) break; for(int i=0;i<5;i++ ) { sprintf(dir,"md %02d\\A%d",n,m); m++; if(system(dir)==1) break; } if(n>5)/////////////// break; } return 0; }
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
#include <io.h> #include <stdio.h> #include <stdlib.h> int main() { int n=0; char dir[12]; int m = 1; while(1) { n++; sprintf(dir,"md %02d >nul",n); if(system(dir)==1) break; for(int i=0;i<5;i++ ) { sprintf(dir,"md %02d\\A%d",n,m); m++; if(system(dir)==1) break; } } return 0; } 这次是对的了,system函数返回1代表失败,这和linux里面的bash是一样的,之前记错了以为0是失败,我调试了下这个程序应该是可以用的
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
引用 10 楼 huyijinai 的回复:
谢谢你的回答,非常感谢了,挺有用得~
不好意思我刚才自己调了下发现还是有问题,我研究下把正确的代码发出来
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
谢谢你的回答,非常感谢了,挺有用得~
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
  
 int main()
 {
     int n=0;
     char dir[12];
	 int m = 1;

     while(1)
     {
         n++;
         sprintf(dir,"md %02d >nul",n);
         if(system(dir)==0)
             break;
		 for(int i=0;i<5;i++ )          
		 {            
			sprintf(dir,"md %02d\\A%d >nul",n,m);            
			m++;             
			if(system(dir)==0)             
			break;            
		 }
     }
     return 0;
 }
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
不好意思我忘记了一点,等你给我改一下
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
你好,上面你的代码我试了下,第一次创建了01文件夹后01文件夹内并没有出现A1-A5这5个文件夹,而第二次运行后出现了02和A1这两个文件,请问下这个要怎么改?
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
你要写的程序只要加一个循环就行了

#include <stdio.h>
 #include <stdlib.h>
  
 int main()
 {
     int n=0;
     char dir[12];
     int m = 1;
     while(1)
     {
         n++;
         sprintf(dir,"md %02d >nul",n);
         if(system(dir)==0)
             break;
         for(int i=0;i<5;i++ )
          {
            sprintf(dir,"md A%d >nul",m);
            m++;
             if(system(dir)==0)
             break;  
           }

     }
     return 0;
 }
 
bbbb007119 2013-07-05
  • 打赏
  • 举报
回复
楼上已经说的很清楚了。。慢了一步,我稍微补充下 system(dir)其实就是调用系统命令,不过这里的系统命令不是通常的dir函数,而是你在前一个语句里面用sprintf构建的一个指令,不如第一次循环,system(dir)=system("md 01 >nul")后面的依次类推。 system函数如果返回0,这也就意味这创建失败,跳出循环
heliwensw 2013-07-05
  • 打赏
  • 举报
回复
for(int=1;i<6;i++) { sprintf(dir,"md A%d >null",i); system(dir); }
heliwensw 2013-07-05
  • 打赏
  • 举报
回复
sprintf(dir,"md %02d >nul",n); 在当前程序的执行目录下调用 md n >null,n=(01..99 00),这个命令共被执行100次,>null就是不输出标准的输出
huyijinai 2013-07-05
  • 打赏
  • 举报
回复
现在要在此基础上改进一下,让程序在第一次运行生成01文件夹的同时在01文件夹内又生成5个名为A1,A2,A3,A4,A5的文件夹。第二次运行的时候生成02文件夹的同时又在02文件夹内生成5个名为A6,A7,A8,A9,A10文件夹。请问各位高手这个程序该怎么写?
加载更多回复(1)

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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