linux系統open、 write、文件操作, ls命令實現

今天主要學習了linux的文件操作,有點心塞的感覺,本身對文件不熟悉加上vim編輯器,感覺很心煩,前兩段代碼已理解調試,第三段是實現linux命令中ls功能的代碼,現在是凌晨零點零九分,先暫時把代碼存一下,明天繼續看。

 

1、標準c函數文件操作,一次最多處理1024個文件

 

/*************************************************************************

> File Name: test.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Tue 27 Jan 2015 09:37:48 AM CST

 ************************************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define MAX 10000000

int main(int argc, char* argv[])

{

FILE** fp_arr = (FILE**)calloc(MAX, sizeof(FILE*));

char buf[1024] = "" ;

int index ;

for(index = 0; index < MAX; index ++)

{

memset(buf, 0, 1024);

sprintf(buf, "%d.txt", index);

fp_arr[index] = fopen(buf, "w");

if(fp_arr[index] == NULL)

{

printf("index: %d\n",index);

break ;

}

}

return 0 ;

}

 

 

 

 

 

 

 

 

 

 

2、使用linux openreadwrite函數操作文件

/*************************************************************************

 *         > File Name: read_file.c

 *         > Author: Comst

 *         > Mail:[email protected]

 *         > Created Time: Tue 27 Jan 2015 10:24:26 AM CST

 *************************************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<fcntl.h>

#include<unistd.h>

int main(int argc, char* argv[])

{

 

int fd_in = open(argv[1],O_RDONLY );

int fd_out = open(argv[2], O_WRONLY | O_TRUNC);

if(fd_in == -1)

{

perror("fail");

exit(1);

}

if(fd_out == -1)

{

perror("out fail");

exit(1);

}

printf("OK!\n");

char buf[128] = "" ;

int iret ;

iret = read(0, buf, 127);

//write(fd_out, buf, strlen(buf));

write(1, buf, strlen(buf));

 

printf("readn: %d\nbuf:%s\n", iret, buf);

close(fd_in);

close(fd_out);

 

return 0 ;

}

 

 

 

 

3ls功能實現,暫時還不太懂。

 

 

/*************************************************************************

> File Name: my_stat.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Tue 27 Jan 2015 03:28:08 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

#include<stdlib.h>

#include<string.h>

#include<grp.h>

#include<pwd.h>

#include<time.h>

void mode_to_str(mode_t md, char* buf) ;

int main(int argc, char* argv[])

{

struct stat my_stat ;

memset(&my_stat, 0, sizeof(my_stat));

char buf[11] ;

 

if(-1 == stat(argv[1], &my_stat))

{

perror("stat");

exit(1);

}

printf("mode: %04x\nlink: %u\nuid: %u\ngid: %u\nsize:%u\nmtime:%u\n",my_stat.st_mode, my_stat.st_nlink, my_stat.st_uid, my_stat.st_gid, my_stat.st_size, my_stat.st_mtime );

mode_to_str(my_stat.st_mode, buf);

 

puts(buf);

printf("%s %s\n", getpwuid(my_stat.st_uid) -> pw_name, getgrgid(my_stat.st_gid) ->gr_name);

printf("%s\n", ctime(&my_stat.st_mtime));

 

 

return 0 ;

}

void mode_to_str(mode_t md, char* buf) 

{

if(S_ISREG(md))

{

buf[0] ='-' ;

}else if(S_ISDIR(md))

{

buf[0] ='d';

}else if(S_ISCHR(md))

{

buf[0] = 'c' ;

}else if(S_ISBLK(md))

{

buf[0]='b';

}else if(S_ISFIFO(md))

{

buf[0] = 'p';

}else if(S_ISLNK(md))

{

buf[0] = 'l' ;

}else 

{

buf[0] = 's' ;

}

if(md & S_IRUSR)

{

buf[1] = 'r' ; 

}else 

{

buf[1] = '-' ;

}

 

if(md & S_IWUSR)

{

buf[2] = 'w' ; 

}else 

{

buf[2] = '-' ;

}

 

if(md & S_IXUSR)

{

buf[3] = 'x' ; 

}else 

{

buf[3] = '-' ;

}

 

 

if(md & S_IRGRP)

{

buf[4] = 'r' ; 

}else 

{

buf[4] = '-' ;

}

 

if(md & S_IWGRP)

{

buf[5] = 'w' ; 

}else 

{

buf[5] = '-' ;

}

 

if(md & S_IXGRP)

{

buf[6] = 'x' ; 

}else 

{

buf[6] = '-' ;

}

 

if(md & S_IROTH)

{

buf[7] = 'r' ; 

}else 

{

buf[7] = '-' ;

}

 

if(md & S_IWOTH)

{

buf[8] = 'w' ; 

}else 

{

buf[8] = '-' ;

}

if(md & S_IXOTH)

{

buf[9] = 'x' ; 

}else 

{

buf[9] = '-' ;

}

buf[10] = 0;

}

 

發佈了26 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章