//in the folder every two seconds to create a file, will in time information written to the file
#include
#include
#include
#include
#include
#include
#include
#include
#include
Int creat_dir (char *);//create folders function
Int creat_file (char *, char *, char *);//create and write function
Int creat_dir (char * pathname)
{
DIR * p;
Int temp.//the mkdir return value
P=opendir (pathname);
If (p=NULL)
{
Printf (" open folder failed, folder does not exist \ n ");
Temp=mkdir (pathname, 0777);
If (temp==1)
{
Printf (" failed to create file \ n ");
return -1;
}
The else
{
Printf (" create folder success \ n ");
return 0;
}
}
The else
{
Printf (" folder exists \ n ");
return 0;
}
}
Int creat_file (char * pathname, char * filename, char * writebuf)
{
Int temp.//chdir return value
int fd;//file identifiers
Int temp_write;//the open function return value
Char wdir [100].//store work path
Temp=chdir (pathname);
If (temp==1)
{
Printf (" switch work path failure \ n ");
return -1;
}
The else
{
If ((getcwd (wdir, 100))==NULL)
{
Failed to get work path printf (" \ n ");
}
Printf (" work path is: % s \ n ", wdir);
Fd=open (filename, O_RDWR | O_CREAT, S_IRWXU);
//to introduced to create a file name
If (fd==1)
{
Printf (" failed to create file \ n ");
return -1;
}
The else
{
Printf (" create file success \ n ");
Temp_write=write (fd, writebuf, strlen (writebuf));
If (temp_write==1)
{
Printf (" write failure \ n ");
Close (fd);
return -1;
}
The else
{
Printf (" write success \ n ");
Close (fd);
return 0;
}
}
}
}
Int main ()
{
Int temp_creat;//create folders function return value
Int temp_file;//create a file return value
Char dirname [10]="DIR";//directory name
Char filename [10]="File";//file name
Char writebuf [15].//the buffer
Time_t timetemp;//time variable
* time_str struct tm;//time structure
Char timebuf [3].//to connect into the time information
Char timebuf_2 [3].
Time (& amp; Timetemp);
Time_str=gmtime (& amp; Timetemp);//in the structure of time
02 sprintf (timebuf_2, "% d", time_str - & gt; Tm_min);
Strcat (dirname, timebuf_2);//folder name to join time information
Temp_creat=creat_dir (dirname);//create folders function called
If (temp_creat==1)//create failure
{
return 1;
}
The else
{
Sprintf (writebuf, "% s \ n", ctime (& amp; Timetemp));//the time information join writebuf
02 sprintf (timebuf, "% d", time_str - & gt; Tm_sec);
Strcat (filename, timebuf);//file name to join time information
Temp_file=creat_file (dirname, filename, writebuf);
//create a file function called
If (temp_file==1)//create failure
{
Return 2;
}
The else
{
return 0;
}
}
}
The running result is
Folder exists
Switch work path failure
Have been abandoned (core dump)
I open the folder where clearly under GDB to debug the folder does not exist why returns a pointer to the p not NULL tangle for a long time the novice asked the great spirit show
CodePudding user response:
Try a program with administrator privileges to perform first, it may not have permission to create the directory or file,CodePudding user response:
Hi Feng,In your ` s code:
If (p=NULL)
You mean to be:
If (p== NULL)
CodePudding user response:
The building Lord now solved? Can you share this problemCodePudding user response: