Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)

简介: Linux下的IO/文件操作练习,知识虽然简单 但是往往基础容易被忽略,偶尔的练习是有必要的。        练习printf /************************************************************************* > File Name: printf.

Linux下的IO/文件操作练习,知识虽然简单 但是往往基础容易被忽略,偶尔的练习是有必要的。

      

练习printf

/*************************************************************************
	> File Name: printf.c
	> Author: 
	> Mail: 
	> Created Time: Wed 11 Feb 2015 01:08:15 AM PST
 ************************************************************************/

#include<stdio.h>
int main()
{ 
    printf("%10s\n","hello");
    printf("%-10s\n","hello");
    printf("%s\n","hello") ;
    printf("%*s\n",10,"hello") ;
    printf("%010d\n",100) ;
    printf("%10.4g\n",1.22) ;
    printf("%10s\n","Hello,I am a programmer !") ;

    return 0 ;
}

练习C标准库的文件复制 相比系统调用  会快 因为有缓冲

/*************************************************************************
	> File Name: cpy.c
	> Author: 
	> Mail: 
	> Created Time: Wed 11 Feb 2015 01:37:56 AM PST
 ************************************************************************/

#include<stdio.h>
int main()
{   
    int c;
    FILE*in=fopen("./printf","r");
    FILE*out=fopen("./printf_tem","w");
    while((c=fgetc(in))!=EOF)
    {
        fputc(c,out) ;
    }

    exit(0);
}

练习查看一个文件的状态

/*************************************************************************
	> File Name: fileinfo.c
	> Author: 
	> Mail: 
	> Created Time: Wed 11 Feb 2015 11:25:21 PM PST
 ************************************************************************/

#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h> 
int main()
{ 
   int fileNo;
   struct  stat fileStat ;
   FILE*pFile= fopen("./cpy","r");
   if(pFile==NULL)
    {
        printf("File Open Error!\n") ;
        exit(0);
    }
   fileNo= fileno(pFile);
   printf("FileNumber:%d\n",fileNo) ;
  if(-1==fstat(fileNo,&fileStat))
    {
        printf("GetFileInfo Error!\n") ;
        exit(0) ;
    }
    printf("DeviceID:%d\n",fileStat.st_dev);
    printf("UserID:%d\n",fileStat.st_uid);
    printf("GroupID:%d\n",fileStat.st_gid);
    printf("FileSize:%d\n",fileStat.st_size);

    return 0 ;
}

我们模拟Linux下的ls程序 

/*************************************************************************
	> File Name: listdir.c
	> Author: 
	> Mail: 
	> Created Time: Thu 12 Feb 2015 12:49:13 AM PST
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
void list_func(char*path,int depth)  
{  
    DIR*pDirHandle= opendir(path);
    struct dirent * dent ;
    struct stat  fstat ;
    if(pDirHandle==NULL)
    {
        printf("OpenDir %s  Error!\n",path);
        exit(0);
    }
   chdir(path);
    while((dent=readdir(pDirHandle))!=NULL)
    {  
        //error then return -1 
        lstat(dent->d_name,&fstat);
        if(S_ISDIR(fstat.st_mode))
        {  
            //remove director . and ..
            if(strcmp(".",dent->d_name)==0||
              strcmp("..",dent->d_name)==0 )
                continue ;
            printf("%*s%s/\n",depth,"",dent->d_name) ;
            list_func(dent->d_name,depth+4) ;
        }else
           printf("%*s%s\n",depth,"",dent->d_name);        
     }
    chdir("..");
    closedir(pDirHandle);
}
int main(int argc,char**argv)
{   
    if(argc<2)
    {
        printf("Param Format:  listdir path\n");
        return ;

    }
    char*pDirPath=argv[1];
    int depath=0;
    printf("List Begin:\n");
    list_func(pDirPath,depath) ;
    printf("List End.\n");
    return 0 ;
}

格式化错误代码

/*************************************************************************
	> File Name: errformat.c
	> Author: 
	> Mail: 
	> Created Time: Fri 13 Feb 2015 12:50:00 AM PST
 ************************************************************************/

#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main()
{  
    char* message=NULL ;
    //error
    fopen("aaaaa","r")  ;
    // printf(strerror(errno));
    message=strerror(errno);
    if(message!=NULL)
        printf("Error:%s\n",message);
    perror("Program:");

    return 0;
}






目录
相关文章
|
17小时前
|
存储 Linux
Linux为新创建的磁盘分区添加文件系统
Linux为新创建的磁盘分区添加文件系统
|
2天前
|
Linux Windows 存储
|
3天前
|
存储 Linux 编译器
【Linux】详解动态库链接和加载&&对可执行程序底层的理解
【Linux】详解动态库链接和加载&&对可执行程序底层的理解
|
3天前
|
存储 算法 Linux
【Linux】详解文件系统以及周边知识
【Linux】详解文件系统以及周边知识
|
3天前
|
Linux C++
【Linux】详解进程程序替换
【Linux】详解进程程序替换
|
4天前
|
存储 缓存 Linux
【Linux】文件系统
在打开文件之前,我们需要找到文件 -> 就要从磁盘中找到对应文件 -> 通过文件路径与文件名
19 4
|
6天前
|
网络协议 Java Linux
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
12 0
|
6天前
|
存储 网络协议 算法
【探索Linux】P.28(网络编程套接字 —— 简单的UDP网络程序模拟实现)
【探索Linux】P.28(网络编程套接字 —— 简单的UDP网络程序模拟实现)
12 0
|
6天前
|
存储 Linux 编译器
【探索Linux】P.13(文件系统 | 软硬链接 | 动态库和静态库)
【探索Linux】P.13(文件系统 | 软硬链接 | 动态库和静态库)
12 0