Home > OS >  Linux c programming problem
Linux c programming problem

Time:10-10

//every one second, create a folder name too +
//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 problem

CodePudding user response:

refer to the second floor bigPillow response:
Hi Feng,

In your ` s code:
If (p=NULL)
You mean to be:
If (p== NULL)

yes thanks

CodePudding user response:

reference HITYM627 reply: 3/f
the building Lord solved this problem now can share

To solve the upstairs said

CodePudding user response:

Write more simple in the shell

CodePudding user response:

This error is lower, but is often made

CodePudding user response:

If judge wrote the if (NULL==p) to prevent more symbols in assignment symbol

CodePudding user response:

Suggest a look upstairs is the veteran

CodePudding user response:

Met a lot of the "=" in CSDN as "==" situation,

CodePudding user response:

All pit, is not the change in the BUG is write a BUG

CodePudding user response:

So what if judgment still need if (NULL==p) to compare safety

CodePudding user response:

refer to the eighth floor windandcloudflying response:
if judge wrote the if (NULL==p) to prevent more symbols in assignment symbol

That's a good suggestion

CodePudding user response:

Practical skills, learning!

CodePudding user response:

Ha ha ha! Taught by

CodePudding user response:

I also have to think with shell lines, or the program directly or move the system call

CodePudding user response:

If (p), the compiler support now, if you remember NULL==p good, of course, but generally want to get up so write, the estimate is written also won't forget,,,

CodePudding user response:

This can be, if the grammar problems inside

CodePudding user response:

Veterans can't write wrong
  • Related