fujinbing 发表于 2013-1-30 20:50:56

c积累

1.调用系统
int main(){
system("rm /home/book/fujinbing");
}
2.得到当前绝对路径,string 赋值,sting数组显示
#include <string>
#include <iostream>
using namespace std;

int main(){
char buf;
getcwd(buf,1024);
printf("%s\n",buf);
string tmppath;
tmppath=buf;
cout<<buf<<endl;
cout<<buf<<endl;
return 1;
}
3.去掉一行 的0x0d回车 0x0a 换行键 0x09 水平置标符 0x20 空格 ,erase 删除
while ( ( tmpsflen > 0 ) && ( ( filepath[ tmpsflen - 1 ] == (char)0x0d )|| ( filepath[ tmpsflen - 1 ] == (char)0x0a )|| ( filepath[ tmpsflen - 1 ] == (char)0x20 )|| ( filepath[ tmpsflen - 1 ] == (char)0x09 ) ) )
{
filepath.erase( tmpsflen - 1 , 1 ) ;
tmpsflen -- ;
}

4.c++方式 得到文件流 读取1行数据
fstream tmpfp ("Config.dat",ios base :: in);
构造 tmpfp实例
string tmpstr       ;
getline( tmpfp , tmpstr )

5.时间,时间格式
#include <time.h>
#include <stdio.h>
int main(){
time_t lt;
printf("%ld\n",lt); //
lt=time(NULL);
printf("%ld\n",lt); //据1900的时间戳
struct tm *st;
st=localtime(&lt);
printf("%d\n",st->tm_sec);
printf("%d\n",st->tm_min);
printf("%d\n",st->tm_hour);
printf("%d\n",st->tm_mday);
printf("%d\n",st->tm_mon);
printf("%d\n",st->tm_year);
printf("%d\n",st->tm_wday);
printf("%d\n",st->tm_yday);
printf("%d\n",st->tm_isdst);
return 1;
}

struct tm
{
int tm_sec;                   /* Seconds.    (1 leap second) */
int tm_min;                   /* Minutes.    */
int tm_hour;                  /* Hours.       */
int tm_mday;                  /* Day.          */
int tm_mon;                   /* Month.       */
int tm_year;                  /* Year - 1900.*/
int tm_wday;                  /* Day of week. */
int tm_yday;                  /* Days in year. */
int tm_isdst;               /* DST.         [-1/0/1]*/

#ifdef__USE_BSD
long int tm_gmtoff;         /* Seconds east of UTC.*/
__const char *tm_zone;      /* Timezone abbreviation.*/
#else
long int __tm_gmtoff;         /* Seconds east of UTC.*/
__const char *__tm_zone;      /* Timezone abbreviation.*/
#endif
};

6.文件属性
1 函数都是获取文件(普通文件,目录,管道,socket,字符,块)的属性。
函数原型
#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf); 提供文件名字,获取文件对应属性。
int fstat(int filedes, struct stat *buf); 通过文件描述符获取文件对应的属性。
int lstat(const char *restrict pathname, struct stat *restrict buf); 连接文件描述符,获取文件属性。

2 文件对应的属性
#include <stdio.h>
#include <string>
#include <sys/stat.h>
using namespace std;
int main(){
struct statat;
string filename ="./aa";

stat(filename.c_str(),&at);

printf("%d\n",at.st_ctime);
return 1;
}
struct stat {   
mode_t   st_mode;       //文件对应的模式,文件,目录等   
ino_t      st_ino;       //inode节点号      
dev_t      st_dev;      //设备号码      
dev_t      st_rdev;       //特殊设备号码   
nlink_t    st_nlink;      //文件的连接数      
uid_t      st_uid;      //文件所有者   
gid_t      st_gid;      //文件所有者对应的组
off_t      st_size;       //普通文件,对应的文件字节数   
time_t   st_atime;      //文件最后被访问的时间      
time_t   st_mtime;      //文件内容最后被修改的时间   
time_t   st_ctime;      //文件状态改变时间      
blksize_t st_blksize;    //文件内容对应的块大小
blkcnt_t   st_blocks;   //伟建内容对应的块数量   
   };


7.编译时链接库 -lpthread链接pthread库。-ltr链接clock_gettime函数相关库 -lssl 链接 md5相关
页: [1]
查看完整版本: c积累