1. 程式人生 > >操作系統文件顯示

操作系統文件顯示

only clas 圖片 == stdio.h int 顯示文件 技術 nbsp

編程顯示文件內容

方法一:

 1 #include<stdio.h>
 2 #include<signal.h>
 3 #include<stdlib.h>
 4 #include<unistd.h>
 5 #include<sys/types.h>
 6 #include<sys/wait.h>
 7 //
 8 
 9 #include<fcntl.h>
10 void main()
11 {
12     int fd,bytes;
13     char ch[513];
14     if((fd=open("
1.txt",O_RDONLY))!=-1) 15 { 16 while((bytes=read(fd,ch,512))==512) 17 { 18 ch[bytes]=\0; 19 printf("%s",ch); 20 } 21 if(bytes<512) 22 { 23 ch[bytes]=\0; 24 } 25 printf("%s",ch); 26 close(fd);
27 } 28 else 29 { 30 printf("ERROR:file open failure!\n文件未找到!\n"); 31 } 32 }

方法二:

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<fcntl.h>
 4 
 5 void main()
 6 {
 7     int fd;
 8     char ch;
 9     if((fd=open("1.txt",O_RDONLY))!=-1)
10     {
11         while
(read(fd,&ch,1)==1) 12 { 13 printf("%c",ch); 14 } 15 close(fd); 16 } 17 else 18 { 19 printf("ERROR:file open failure!\n文件未找到!\n"); 20 } 21 }

結果

技術分享圖片

操作系統文件顯示