Hello everybody!

I am trying to create folder's which will contain files and lastly all these files will contain texts.

My problem is that am not able to create this folder into a specific location or pathname!!!

Here is the code, please advice accordingly:

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

int main() {
        int i;
		//char pathname = "E:\Visual C\test";
        char dirname[50];

        for(i=0;i<10;i++) {
                sprintf(dirname,"test%d",i);
                if((mkdir(dirname,00777))==-1) {
                        fprintf(stdout,"error in creating dir\n");
                }
        }
}

Thanks

Recommended Answers

All 4 Replies

You have to specify the full path to the new folder, otherwise it will just get created in the current working directory. sprintf(dirname,"/usr/include/somewhere/test%d",i);

You have to specify the full path to the new folder, otherwise it will just get created in the current working directory. sprintf(dirname,"/usr/include/somewhere/test%d",i);

Thanks 'Ancient Dragon', modified code:

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

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

        for(i=0;i<10;i++) {			 
				  sprintf(dirname,"E:/Visual C/practice/practice/hello/test%d",i);
                   if((mkdir(dirname,00777))==-1) {
                        fprintf(stdout,"error in creating dir\n");
                }
        }
}

Now my second issue :$ is that I would also like to create files inside each of these folders.

Here is my code on how to create files with text in them but I would like to place them inside the 'Folders' i.e. test0, test1....etc

main( )
{
FILE *fp;
FILE *patt,*patt1,*patt2,*patt3;
char stuff[25];
int index;

mkdir(const char *path, mode_t mode);

static const char text[26] = "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" 
                             "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z";

	  

fp = fopen("DATA.TXT","w"); /* open for writing */
patt = fopen("PATTERN.TXT","w"); /* open for writing */
patt1 = fopen("PATTERN1.TXT","w");
patt2 = fopen("PATTERN2.TXT","w");
patt3 = fopen("PATTERN3.TXT","w");
fprintf(patt,"ABCDEFGHIJ");
fprintf(patt1,"ZYXWVUTSRQ");
fprintf(patt2,"ZZZZZZZZZZ");
fprintf(patt3,"APKMNGHQUE");

for (index = 1;index <= 10000;index++)
fprintf(fp,text);
fclose(fp);
fclose(patt);/* close the file before ending program */
fclose(patt1);
fclose(patt2);
fclose(patt3);
}

Pattern file would probably remain the same but the length of text inside the data file will change

Thanks 'Ancient Dragon', modified code:

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

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

        for(i=0;i<10;i++) {			 
				  sprintf(dirname,"E:/Visual C/practice/practice/hello/test%d",i);
                   if((mkdir(dirname,00777))==-1) {
                        fprintf(stdout,"error in creating dir\n");
                }
        }
}

Now my second issue :$ is that I would also like to create files inside each of these folders.

Here is my code on how to create files with text in them but I would like to place them inside the 'Folders' i.e. test0, test1....etc

main( )
{
FILE *fp;
FILE *patt,*patt1,*patt2,*patt3;
char stuff[25];
int index;

mkdir(const char *path, mode_t mode);

static const char text[26] = "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" 
                             "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z";

	  

fp = fopen("DATA.TXT","w"); /* open for writing */
patt = fopen("PATTERN.TXT","w"); /* open for writing */
patt1 = fopen("PATTERN1.TXT","w");
patt2 = fopen("PATTERN2.TXT","w");
patt3 = fopen("PATTERN3.TXT","w");
fprintf(patt,"ABCDEFGHIJ");
fprintf(patt1,"ZYXWVUTSRQ");
fprintf(patt2,"ZZZZZZZZZZ");
fprintf(patt3,"APKMNGHQUE");

for (index = 1;index <= 10000;index++)
fprintf(fp,text);
fclose(fp);
fclose(patt);/* close the file before ending program */
fclose(patt1);
fclose(patt2);
fclose(patt3);
}

Pattern file would probably remain the same but the length of text inside the data file will change

Quick question, Why are you using the for loop ?
Will the length of the the text array be dynamic or static ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.