Linux under C programming-----io/file operation simulation Linux LS program display file system tree structure (2)

Source: Internet
Author: User

The io/file operation exercises under Linux, although the knowledge is simple but often the foundation is easy to overlook, the occasional practice is necessary.

Practice printf

/*************************************************************************> File name:printf.c> Author: > Mail: > Created time:wed 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", "Ten," "Hello");    printf ("%010d\n", +);    printf ("%10.4g\n", 1.22);    printf ("%10s\n", "Hello,i am a programmer!");    return 0;}

Practice C Standard library File replication is faster than system calls because there is buffering

/*************************************************************************> File name:cpy.c> Author: > Mail: > Created time:wed 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 ())!=eof)    {        FPUTC (c,out);    }    Exit (0);}

Practice Viewing the status of a file

/*************************************************************************> File name:fileinfo.c> Author: > Mail: > Created time:wed 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;}

we simulate the LS program under Linux

/*************************************************************************> File name:listdir.c> Author: > Mail: > Created time:thu 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 (PA    TH);    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-&GT;d_name);        List_func (DENT-&GT;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;}






Linux under C programming-----io/file operation simulation Linux LS program display file system tree structure (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.