设为首页 加入收藏

TOP

c指针(29):C应用技巧(一)
2014-03-10 13:04:30 来源: 作者: 【 】 浏览:320
Tags:指针 应用技巧

    字符串小写转大写
    #include <stdio>
    int main(void)
    {
    int i=0;
    char string[100];
    strcpy(string,"abcdefghijklmnopqrstuvwxyz");
    while (string[i]!='\0'){ //将小写转化成大写
    if (islower(string[i]))
    string[i]=toupper(string[i]);
    i++;
    }
    printf("%s\n",string);
    return 0;
    }
    linux-C获得用户信息和节点信息
    C代码
    #include <unistd.h>
    #include <sys/utsname.h>
    #include <sys/types.h>
    #include <pwd.h>
    int main(void){//
    char hname[256];//节点名称
    struct utsname uts;//节点结构信
    uid_t uid;
    gid_t gid;
    struct passwd *pw;
    if (gethostname(hname,255)!=0||uname(&uts)<0){
    printf("不能得到主机信息");
    exit(1);
    }
    printf("主机名:%s\n",hname);
    printf("系统名称:%s\n 机器名称:%s\n",uts.sysname,uts.machine);
    printf("节点名称:%s\n",uts.nodename);
    printf("版本:%s,版本号%s",uts.release,uts.version);//系统版本,版本号
    //取得当前用户登陆情况
    uid=getuid();
    gid=getgid();
    pw=getpwuid(uid);
    printf("用户id 为%d,用户组为%d\n",uid,gid);
    printf("用户真实姓名%s\n用户名称:%s\n",pw->pw_gecos,pw->pw_name);
    printf("用户uid:%s\ngid:%s\n",pw->pw_uid,pw->pw_gid);
    printf("主目录:%s\n",pw->pw_dir);
    printf("用户shell:%s\n",pw->pw_shell);
    }
    运行:
    # gcc -o test7 test7.c
    test7.c: In function 'main':
    test7.c:13: warning: incompatible implicit declaration of built-in function 'printf'
    test7.c:14: warning: incompatible implicit declaration of built-in function 'exit'
    test7.c:17: warning: incompatible implicit declaration of built-in function 'printf'
    # ./test7
    主机名:puppypc
    系统名称:Linux
    机器名称:i686
    节点名称:puppypc
    版本:2.6.30.5,版本号#1 SMP Tue Sep 1 15:48:26 GMT-8 2009用户id 为0,用户组为0
    用户真实姓名root
    用户名称:root
    用户uid:(null)
    gid:(null)
    主目录:/root
    用户shell:/bin/sh
    用户密码:x
    #
    linux-C产生临时文件
    #include <stdio.h>
    int main(void){
    char tmpname[L_tmpnam];
    char *filename;
    FILE *fp;
    strcpy(tmpname,"/tmp/dfXXXXXX");//file name:df…
    filename=mktemp(tmpname);//generate tempfile
    printf("temporary file name:%s\n",filename);
    fp=tmpfile();
    if (fp) printf("temporary file oepn.\n");
    else perror("error");
    exit(0);
    }
    linux-文件属性及目录基本操作
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <dirent.h>
    int main(int argc,char *argv[]){
    int i;
    struct stat buf;
    char *ptr;
    if (argc<2){
    printf("filename error");
    exit(1);
    }
    if (lstat(argv ,&buf)<0){//lstat和stat 判断文件属性,但lstat只判断连接文件本身,不追踪真实文件
    perror("lstat error");
    }
    if (S_ISREG(buf.st_mode)) ptr="普通文件";
    else if(S_ISDIR(buf.st_mode)) ptr="目录";
    else if(S_ISCHR(buf.st_mode)) ptr="字符设备";
    else if(S_ISFIFO(buf.st_mode)) ptr="有名管道";
    else if(S_ISLNK(buf.st_mode)) ptr="符号链接";
    else if(S_ISBLK(buf.st_mode)) ptr="块设备";
    else if(S_ISSOCK(buf.st_mode)) ptr="SOCKET";
    else ptr="未知设备";
    printf("FILE %s is %s file",argv ,ptr);
    if (S_ISREG(buf.st_mode)){//如果是文件名
    printf("file size is %d bytes.\n",buf.st_size);
    }
    if (S_ISDIR(buf.st_mode)){//如果是目录名,则ls目录的文件
    DIR *dp;
    struct dirent *dirp;
    if ((dp=opendir(argv ))==NULL) perror("opendir error");
    while ((dirp=readdir(dp))!=NULL){
    printf("%s\n",dirp->d_name);   //输出目录下的文件名
    }
    closedir(dp);
    free(dirp);
    }
    return 0;
    }
    c-文件操作-文件位置
    CC++C#FP
    long file_pos=ftell(fp);//返回当前文件位置
    fgetpos(fp,&fp_pos);//返回当前文件位置到fp_pos中
    fsetpos(fp,&fp_pos);//设置当前文件位置为fp_pos
    rewind(fp);//把文件置在起始处
    linux-多线程-互斥锁在多进程共享
    C代码
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void){//2个进程,一个进程完成每次加1,另一个进程完成每次加2,2个进程协作完成累加,使用共享内存方式在进程间通信
    int *x;
    int rt;
    int shm_id;
    char *addnum="myadd";
    char *ptr;
    pthread_mutex_t mutex;//互斥对象
    pthread_mutexattr_t mutexattr;//互斥对象属性
    pthread_mutexattr_init(&mutexattr);//初始化互斥对象属性
    pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);//设置互斥对象为PTHREAD_PROCESS_SHARED共享,即可以在多个进程的线程访问,PTHREAD_PROCESS_PRIVATE为同一进程的线程共享

     

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言条件编译 下一篇c指针(28):LINUX应用(4)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: