I'm trying to create a folder using mkdir in C but it wont't work
the code won't create the folders
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
int main (){
char chemin[256];
char name[20];
//char fichier[100];
// FILE *fp;
strcpy(chemin,"/home/Deva/Documents/prog/C/la/sds");
mkdir(chemin,0755);
if (mkdir(chemin,0755)==-1){
printf("\nERROR\n");
} else { printf("fichier creer"); }
}
You have to create the intermediate directories first: e.g. you must create /home/Deva before creating /home/Deva/Documents, etc.
you are passing PATH not just directory name.
so please check below post would help
Recursive mkdir() system call on Unix
2 things:
1) You need to create the intermediate directories first.
2) You cannot create a directory twice, which you do here:
mkdir(chemin,0755);
if (mkdir(chemin,0755)==-1){
Related
I'm making a program, and it's supposed to run a file in a different directory (/files/runme.c). How can I run this file in C?
I have tried the system() function, however this does not work.
MAIN.c:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("runme.c");
return 0;
}
runme.c:
#include <stdio.h>
int runme() {
printf("hello world");
}
My expected result is:
hello world
I get:
exit status -1
I want it to run everything inside the contents of runme.c. How can I do this (on Windows and Linux)?
To get the runme() function from a different file to be passed to your main , you need to create a header file with the prototype of the runme() function in it, include this header file in main.c and compile using both files.
main.h:
int runme(void);
main.c
#include <stdio.h>
#include <stdlib.h>
#include "main.h" //main.h needs to be in the same directory as main.c
int main(void) {
runme();
return 0;
}
runme.c
#include <stdio.h>
int runme() {
printf("hello world");
}
Finally compile:
gcc main.c {path}/runme.c
from system() you can run .exe
as far as I know not .c.
Just compile the .c file you want to run put it on same folder as of file containing the main()
eg.
second_program.c->compiled->copy the generated .exe file to the same folder and then use the system(second_program.exe)
or you have to create two file second_program.h for header files and functions prototype etc
and second_program.c for their definition and then use include
if the files are not in the same folder as of main()
then you have to add the location of second_program in your program properties
or add include "second_program.h" if files exist in same folder
To All IT Gurus,
I am trying to execute 2 .C files in one project in Eclipse. I get error. So, for example, Let us say I have this below code.
Both files 1 and 2 are in the same project First C Project, Is this allowed.??
Project Name - First_C_Project
File 1 = Example.C
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void) {
printf ("C Programming, This is my First C Program");
return EXIT_SUCCESS;
}
File 2: TestProgram.C
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void) {
printf ("This is a TEST C Program, Check it out. ");
return 0;
}
I created a new project with a File 2 C file. And that works. But why can't I put the 2 C files work within the same First_C_Project project ???
The problem is not that you have 2 c files, but that you have 2 "main" functions.
If you want to have more than one main, look here:
Eclipse CDT : How to manage multiple main() functions in a single C++ project?
If you don't need two main functions, simply comment/rename/delete the on you are not using.
Basically, this should be a simple piece of code that opens a directory stream and looks for symbolic links. Whenever a symbolic link is found, it should print ("Symbolic link found");
However, the lstat(dirp->d_name,&bufcall always returns a value < 0, and I don't know why.
I created the two symbolic link opening the file folder, opening a terminal window inside the folder and running
ln -s ciao.txt link1 and
ln -s ciao2.txt link2
I know I should call closedir() later in my code, please don't care about this.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
void main (int argc, char* argv[])
{
char buffer[100],dir[100];
struct stat buf;
int x;
DIR *dp;
struct dirent *dirp;
if((dp=opendir(argv[1]))==NULL)
{
printf("\nError opening directory stream, now exiting...\n");
exit(-1);
}
while((dirp=readdir(dp))!=NULL)
{
lstat(dirp->d_name,&buf);
if(S_ISLNK(buf.st_mode))
printf("\n%s Is a symbolic link\n",dirp->d_name);
else
printf("\n%s Is not a symbolic link\n",dirp->d_name);
}
}
Some help would be appreciated. Thanks.
d_name is the file name in the directory, not a full path name. You must eather chdir into the directory you are looking at, or construct full path names for the files.
The simplest solution is to add this line just before your while loop:
chdir(argv[1]);
I am trying to create a directory using the following code. It compiles, but it does not create a directory. Any suggestions?
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int main(void)
{
const char base[] = "filename";
char filename [ FILENAME_MAX ];
int number = 42;
sprintf(filename, "%s/%d", base, number);
printf("filename = \"%s\"\n", filename);
mkdir (filename, S_IRWXU);
return 0;
}
Does the "filename" directory already exist? mkdir() will only create one directory at a time; if the parent directory doesn't exist either, you'll have to create it separately, first.
Most probably it fails to create directory because you are trying to create a nested directory and its parent does not exist. mkdir cannot create directories recursively. But you can only guess unless you properly check return codes and errors in your program.
I just want to write a program which takes a directory name as argument
Validate that it is in fact a directory
Get a listing of all files in the directory and print it
The word directory doesn't even appear in the C standard. This is an OS concept.
Look at stat. It will provide you with the information you want; all you have to do is interpret it.
Edit: A brief example.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int is_dir(char *path)
{
struct stat dir_stats;
stat(path, &dir_stats);
if (S_ISDIR(dir_stats.st_mode))
return TRUE;
return FALSE;
}
For the list of files in the directory, use readdir.