Using a C program to create directories in UNIX


 
Thread Tools Search this Thread
Top Forums Programming Using a C program to create directories in UNIX
# 1  
Old 11-13-2005
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
# 2  
Old 11-14-2005
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);
        }
}

# 3  
Old 11-14-2005
Using system() to invoke the mkdir program is rather expensive. Why not just use the mkdir system call? It would be much faster.
# 4  
Old 11-14-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to create automated Directories

Hi, On serverB i wish to have a script that creates ONLY & EXACTLY the same folder structure that i provide on ServerA. Thus if serverA has a folder "Output" under /opt/app/Output and has the below folders under Output Output Output/logs Output/reciever Output/data... (11 Replies)
Discussion started by: mohtashims
11 Replies

2. UNIX for Dummies Questions & Answers

Create 2 directories in one command

Hi how can i create 2 directories in two different directories ($HOME and $PWD) with 1 command? dir 1 in $HOME and dir2 in $PWD (2 Replies)
Discussion started by: chinababy
2 Replies

3. Shell Programming and Scripting

For Loop Range Create Directories

Hello, I am a bit stumped on this. I am attempting to create 24 empty directories with a loop. Seems like I have incorrect syntax. When I run the following command I get the error below. Command $ for i in {2..24}; do mkdir $i_MAY_2011 ; doneError x 24 mkdir: missing operand Try `mkdir... (2 Replies)
Discussion started by: jaysunn
2 Replies

4. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

5. UNIX for Dummies Questions & Answers

Want to create 3 different new directories under the same path

Hi, Iam new to UNIX...My requirement is to create 3 dir as an hierarchy under /var/opt/temip.The output should be /var/opt/temip/GP_Int/GPTTS/AUTO. I have tried the following script...But only GP_int folder is getting created and not other folders...Can someone help??? #!/usr/bin/ksh #script... (1 Reply)
Discussion started by: Llb
1 Replies

6. UNIX for Dummies Questions & Answers

How to create shotcuts to the directories

Hi, I need your help in writing shortcuts to my directories. So that I can go into the directories with the help of shortcuts. For example: there is a directory called /home/java/webapps/project1 I want to give a shortcut as project1 . So whenever I have give cd project 1 from command line ... (3 Replies)
Discussion started by: TonySolarisAdmi
3 Replies

7. UNIX for Advanced & Expert Users

How to create short form for directories?

Hi Friends, Can you please tell me how to create short form for directories? like, this is a directory: /usr/tmp/progs/scripts when i give cd $short_name, it should take to the above path. in which env setting do i have to set? Thanks, Rashmy. (8 Replies)
Discussion started by: smr_rashmy
8 Replies

8. Programming

How to create a new unix user in through a c program

Hi , I want to create a new user using c program not with unix adduser command . is it possible to write a cprogram to create a new user account , it should accept username , grouid , group name and all other privilages . i can use system calls inside c program to do this . i will... (5 Replies)
Discussion started by: naren_chella
5 Replies

9. UNIX for Dummies Questions & Answers

How to create directories

Hi... Can any1 help me by telling me the way to create multiple directories using single command.... to create 1 directory.. mkdir is used.... :D but how to create multiple direcs. like 4 direc. i tried .... $ mkdir a; mkdir b; mkdir c; mkdir d But its 4 commands in a single... (3 Replies)
Discussion started by: abishekmag
3 Replies

10. UNIX for Dummies Questions & Answers

How do I create & Maintain directories

I need to either create or use a current directory to provide a path for a windows file, how do I do that? kimj (4 Replies)
Discussion started by: kimjones142001
4 Replies
Login or Register to Ask a Question