LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2014, 08:34 PM   #1
physicist2011
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Rep: Reputation: Disabled
making directory trees in C


Hi,
I am trying to create a directory tree to organize output from a simulation.
I am trying it two different ways but I am having issues with both.
The first is to use the system command with something like this

i and j are ints ()


//this inside a larger for loop
for (i=1;i<imax;i++){


for(j=1;j<jmax;j++){

.....
.....


char dirname1[150],dirname2[150],dirname3[150],dirname4[150],




sprintf(dirname1,"mkdir /home/test/program/directory%d/ \n",k);


if(system(dirname1)==-1){fprintf(stdout,"error in creating dir\n");exit(EXIT_FAILURE);}



sprintf(dirname2,"/home/test/program/directory%d/Data%d \n",k,p);
if(system(dirname2)==-1){fprintf(stdout,"error in creating subdir\n");exit(EXIT_FAILURE);}

...This works (at first) as I cycle though values of i and j but after awhile I get to a certain iteration in i or j the system call starts failing (I dont know why.)


The second way I have tried is with the mkdir function:

sprintf(dirname1,"mkdir /home/test/program/directory%d/ \n",k);

printf("\n%s\n",dirname1);


if(mkdir(dirname1, 0777)==-1){fprintf(stderr," error in writing permissions errno=%d \n",errno);}


sprintf(dirname2,"/home/test/program/directory%d/Data%d \n",k,p);


if(mkdir(dirname2,0777)==-1){fprintf(stdout,"error in creating subdirectory errno=%d\n",errno);exit(EXIT_FAILURE);}


.. The problem here is that mkdir fails and I get errno 2.
If anyone can enlighten me to the correct way to make this directory tree I would greatly appreciate it.
 
Old 08-05-2014, 09:56 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

errno 2 usually means "No such file or directory". Are you sure the parent directory exists?

Evo2.
 
Old 08-05-2014, 10:11 PM   #3
physicist2011
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes,
The directory appears to exist.
For some reason mkdir can't find it.
 
Old 08-05-2014, 10:28 PM   #4
turtleli
Member
 
Registered: Aug 2012
Location: UK
Posts: 206

Rep: Reputation: Disabled
Quote:
Originally Posted by physicist2011 View Post
sprintf(dirname1,"mkdir /home/test/program/directory%d/ \n",k);
sprintf(dirname2,"/home/test/program/directory%d/Data%d \n",k,p);
I believe you were intending to do:
Code:
sprintf(dirname1,"/home/test/program/directory%d",k);
sprintf(dirname2,"/home/test/program/directory%d/Data%d",k,p);
 
Old 08-05-2014, 10:35 PM   #5
physicist2011
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes,
you are correct.
I have the code correct in the original program though.
 
Old 08-05-2014, 10:52 PM   #6
turtleli
Member
 
Registered: Aug 2012
Location: UK
Posts: 206

Rep: Reputation: Disabled
Can you copy and paste the relevant code from your program then (please use the CODE tags)?
 
Old 08-06-2014, 12:19 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you can use the system call mkdir() instead of system(). You can use perror() to print error message after mkdir().
Also you can try system("mkdir -p ......")
 
1 members found this post helpful.
Old 08-06-2014, 10:30 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
using system is a disciplinary offence.

I would consider firing anyone who uses it without a good explanation.

 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Major Directory Trees Ztcoracat Linux - General 7 08-25-2012 07:40 PM
[SOLVED] Same files on distinct directory trees 0p3r4t4 Linux - General 1 11-25-2011 05:01 AM
Merge directory trees mrwall-e Linux - Software 5 02-20-2011 08:37 AM
Q: on deleting directory trees n00bJoe Arch 3 03-09-2006 12:13 PM
How to keep 2 directory trees synchronised? hrr Linux - General 2 07-02-2003 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration