int my_log(const char *format,...)
{
va_list args;
FILE *fp;
fp = fopen("/tmp/my_log","a+");
if(!fp)
{
fprintf(stderr,"fp is NULL\n");
return -1;
}
va_start(args,format);
vfprintf(fp,format,args);//当写入文件流的字符只有一行时推荐用fprintf而不是fread,vfprintf代表可变参数形式
va_end(args);
fclose(fp);
return 0;
}
从一个文件复制到另一个文件
</pre><pre name="code" class="cpp">int file_copy(char *fromfile,char *tofile)
{
FLIE *from,*to;
char tmp[1024];
from = fopen(fromfile,"r");
to = fopen(tofile,"w+");
while(fgets(tmp,1024,from)!=NULL)//fgets一次一行,fread一次读整个文件
{
fwrite(tmp,sizeof(tmp),to);
}
fclose(from);
fclose (to);
return 0;
}
调用形式file_copy("/tmp/A","/tmp/B")
将文件流中的信息提取出来并添加值
</pre><p></p><pre name="code" class="cpp">while(fgets(buf,sizeof(buf),buf))
{
if(strstr(buf,"hello"))
{
snprintf(buf,sizeof(buf),"hello = %s",p);
}
}
读目录判断目录是否含有某个文件
重要的几部:
DIR dir = NULL;
dir = opendir("/tmp/dir");
while((ent = readdir(dir)))
{
strncmp(ent->d_name,"hello",strlen("hello"))
}
malloc之后一般要memset设空间值为0