470,573 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

How to create multiple files inside a folder in C?

Hi ALL,

I 'm using DevC++ for writing a C source code to create multiple files and folders under a directory.With the help of mkdir command i am able to get a bunch of 40 folders in the current working directory.I have given here my code.


Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.       char folderNames[40][3];
  4.       int i,fptr;
  5.       mode_t dir_mode = S_IRWXU;
  6.       char *temp_name;
  7.  
  8.       for (i = 0 ; i < 40; i++)
  9.       {
  10.            sprintf(folderNames[i],"%d",(i+1));
  11.       }
  12.  
  13.  
  14.       for(i=0;i<40;i++)
  15.       {
  16.        temp_name = folderNames[i];
  17.        fptr = mkdir(temp_name,dir_mode); 
  18.       }
  19.  
  20.       (fptr == 0)? goodbye():error_msg();     
  21.       fflush(stdin);
  22.       getchar();
  23. }
What do I struggle now is to get into each of the folders and create files?How can I change the program control from the present working directory into the desired folders?I tried with system("cd 1")..but its not working..It gives the place where the program resides.

Some one Plz help me..

Thanks,
Rama
May 17 '07 #1
1 5154
AdrianH
1,251 Expert 1GB
What do I struggle now is to get into each of the folders and create files?How can I change the program control from the present working directory into the desired folders?I tried with system("cd 1")..but its not working..It gives the place where the program resides.

Some one Plz help me..

Thanks,
Rama
The current working directory is just a state (of mind as it were) to any process. And because system spawns a process, only that process only knows what state it is in. The system command will just not cut it.

See this link for the info you seek.


Adrian
May 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Terry Holland | last post: by
1 post views Thread by larryawade | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.