The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create Virtual directories on Webserver vr76413 SUN Solaris 0 06-28-2007 09:24 AM
Create Folder in Multiple Directories Stud33 Shell Programming and Scripting 15 07-20-2006 12:44 PM
How to create a new unix user in through a c program naren_chella High Level Programming 5 03-02-2006 09:39 PM
How to create directories abishekmag UNIX for Dummies Questions & Answers 3 03-21-2005 10:34 AM
How do I create & Maintain directories kimjones142001 UNIX for Dummies Questions & Answers 4 09-10-2001 02:40 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-13-2005
Registered User
 

Join Date: Nov 2005
Posts: 2
Using a C program to create directories in UNIX

Aloha,

I'm attempting to use a C program to create directories and then use a system call to have another program write .dat files into that directory. I understand that I could use the "system("mkdir directory_name")" function however, I would like my program to create a new directory each time the program loops by incrementing an integer contained in the file name each loop. Here are some example directory names: A1, A2, A3, A4, A5...

I know that this isn't the proper use of the system function, but below is some code that might better illustrate what im trying to do.

main()
{
int i;
for(i=0;i<5;i++)
system("mkdir A%d",i);
}

I also don't mind using a string of characters in an array that I would modify each time I pass through the loop and use as the directory's name.

Does anyone know of a simple way to have a C program create unique directories each time it passes through a loop, by incrementing an integer contained in the file name?

Mahalo,
Windell
Reply With Quote
Forum Sponsor
  #2  
Old 11-13-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
You are on the right track, just have to change the syntax of system. You have to do something like this:
Code:
#include<stdio.h>
#include<stdlib.h>

int main() {
        int i;
        char command[50];
        for(i=0;i<5;i++) {
                sprintf(command,"mkdir A%d",i);
                system(command);
        }
}
Reply With Quote
  #3  
Old 11-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Using system() to invoke the mkdir program is rather expensive. Why not just use the mkdir system call? It would be much faster.
Reply With Quote
  #4  
Old 11-13-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
I thought that I'd just fix the OP's code, rather than present a totally new approach. But, here goes:

Code:
#include<stdio.h>
#include<sys/stat.h>

int main() {
        int i;
        char dirname[50];

        for(i=0;i<10;i++) {
                sprintf(dirname,"A%d",i);
                if((mkdir(dirname,00777))==-1) {
                        fprintf(stdout,"error in creating dir\n");
                }
        }
}
Note: only rudimentary error checking has been implemented in above code.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0