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;
}






目录
相关文章
|
6天前
|
存储 缓存 Linux
【Linux】文件系统
在打开文件之前,我们需要找到文件 -> 就要从磁盘中找到对应文件 -> 通过文件路径与文件名
21 4
|
4天前
|
存储 Linux C语言
|
6天前
|
Linux C语言 调度
|
6天前
|
存储 Linux
Linux为新创建的磁盘分区添加文件系统
Linux为新创建的磁盘分区添加文件系统
|
6天前
|
Linux Windows 存储
|
6天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
6天前
|
存储 算法 Linux
【Linux】详解文件系统以及周边知识
【Linux】详解文件系统以及周边知识
|
6天前
|
Linux C语言
【Linux】 拿下 系统 基础文件操作!!!
怎么样,我们的猜测没有问题!!!所以语言层的文件操作函数,本质底层是对系统调用的封装!通过不同标志位的封装来体现w r a+等不同打开类型! 我们在使用文件操作时,一般都要使用语言层的系统调用,来保证代码的可移植性。因为不同系统的系统调用可以会不一样!
18 2
|
6天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
13 0
|
6天前
|
存储 缓存 安全
Java 中 IO 流、File文件
Java 中 IO 流、File文件