问题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
参考文档