問題1:利用讀寫特性,write寫入某個數據之後,將其read讀取出來
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int openFile ( const char * filePath) ;
void readFile ( int fd) ;
int writeFile ( int fd, char * str) ;
int backToStart ( int fd) ;
int WRFile ( const char * filePath, const char * dataSource) ;
int main ( void ) {
WRFile ( "1.txt" , "this is a test data" ) ;
return 0 ;
}
int openFile ( const char * filePath) {
printf ( "正在打開%s文件····\n\n" , filePath) ;
int fd = open ( filePath, O_RDWR) ;
if ( fd == - 1 ) {
perror ( "打開失敗!\n\n" ) ;
return - 1 ;
}
printf ( "打開成功 !\n\n" ) ;
return fd;
}
void readFile ( int fd) {
char c[ 2 ] = { 0 } ;
int flag = 1 ;
printf ( "正在讀取····\n\n" ) ;
do {
flag = read ( fd, c, sizeof ( c) - 1 ) ;
if ( flag > 0 ) {
printf ( "%s" , c) ;
}
} while ( flag) ;
printf ( "\n\n讀取成功 !\n\n" ) ;
}
int writeFile ( int fd, char * str) {
printf ( "正在寫入····\n\n" ) ;
printf ( "%s\n\n" , str) ;
int w = write ( fd, str, strlen ( str) ) ;
printf ( "寫入成功 !\n\n" ) ;
return w;
}
int backToStart ( int fd) {
return lseek ( fd, 0 , SEEK_SET ) ;
}
int WRFile ( const char * filePath, const char * dataSource) {
int fd = openFile ( filePath) ;
writeFile ( fd, dataSource) ;
backToStart ( fd) ;
readFile ( fd) ;
close ( fd) ;
}
even@ubuntu:~/Desktop/shared/day05$ ls
1.txt 2.txt CopyFile.c WR.c
even@ubuntu:~/Desktop/shared/day05$ gcc WR.c -o main1
even@ubuntu:~/Desktop/shared/day05$ ./main1
正在打開1.txt文件····
打開成功 !
正在寫入····
this is a test data
寫入成功 !
正在讀取····
this is a test data
讀取成功 !
even@ubuntu:~/Desktop/shared/day05$
this is a test data
問題2:實現複製功能,將文件1的數據複製到文件2中
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int openFile ( const char * filePath) ;
int getFileSize ( int fd) ;
int copyFile ( const char * sourceFile, const char * targetFile) ;
int main ( void ) {
copyFile ( "1.txt" , "2.txt" ) ;
return 0 ;
}
int openFile ( const char * filePath) {
printf ( "正在打開%s文件····\n\n" , filePath) ;
int fd = open ( filePath, O_RDWR) ;
if ( fd == - 1 ) {
perror ( "打開失敗!\n\n" ) ;
return - 1 ;
}
printf ( "打開成功 !\n\n" ) ;
return fd;
}
int getFileSize ( int fd) {
int size = lseek ( fd, 0 , SEEK_END ) ;
lseek ( fd, 0 , SEEK_SET ) ;
return size;
}
int copyFile ( const char * sourceFile, const char * targetFile) {
int sourFD = openFile ( sourceFile) ;
int targFD = openFile ( targetFile) ;
int fileSize = getFileSize ( sourFD) + 1 ;
char str[ fileSize] ;
printf ( "正在讀取數據源··· \n\n" ) ;
read ( sourFD, str, sizeof ( str) ) ;
printf ( "%s\n\n" , str) ;
printf ( "讀取成功 !\n\n" ) ;
printf ( "正在寫入目標源··· \n\n" ) ;
printf ( "%s\n\n" , str) ;
write ( targFD, str, strlen ( str) ) ;
printf ( "寫入成功 !\n\n" ) ;
close ( sourFD) ;
close ( targFD) ;
}
even@ubuntu:~/Desktop/shared/day05$ ls
1.txt 2.txt CopyFile.c main1 WR.c
even@ubuntu:~/Desktop/shared/day05$ gcc CopyFile.c -o main2
even@ubuntu:~/Desktop/shared/day05$
even@ubuntu:~/Desktop/shared/day05$ ./main2
正在打開1.txt文件····
打開成功 !
正在打開2.txt文件····
打開成功 !
正在讀取數據源···
this is a test data
讀取成功 !
正在寫入目標源···
this is a test data
寫入成功 !
even@ubuntu:~/Desktop/shared/day05$
this is a test data
this is a test data
參考文檔