File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed
Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ CMAKE_MINIMUM_REQUIRED (VERSION 2.9)
2+ PROJECT (fd)
3+ INCLUDE_DIRECTORIES (./include )
4+ AUX_SOURCE_DIRECTORY (./src DIR_SRC)
5+ ADD_EXECUTABLE (./bin ${DIR_SRC} )
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <unistd.h>
3+ #include <sys/types.h>
4+ #include <sys/stat.h>
5+ #include <fcntl.h>
6+
Original file line number Diff line number Diff line change 1+ #include "../include/head.h"
2+ int main (){
3+ char * path = "file.txt" ;
4+ int fd ;
5+ char buf [40 ],buf2 []= "hello world" ;
6+ int n ,i ;
7+ if ((fd = open (path ,O_RDWR ))< 0 ){
8+ perror ("open file failed" );
9+ return 1 ;
10+ }
11+ else
12+ {
13+ printf ("open file successfully\n" );
14+ }
15+
16+ if ((n = read (fd ,buf ,20 ))< 0 ){
17+ perror ("read failed" );
18+ return 1 ;
19+ }
20+ else
21+ {
22+ printf ("output read data:\n" );
23+ printf ("%s\n" ,buf );
24+ }
25+ if ((i = lseek (fd ,11 ,SEEK_SET ))< 0 ){
26+ perror ("lseek error" );
27+ return 1 ;
28+ }
29+ else
30+ {
31+ if (write (fd ,buf2 ,11 )< 0 ){
32+ perror ("write error" );
33+ return 1 ;
34+ }
35+ else
36+ {
37+ printf ("write successfully\n" );
38+ }
39+ }
40+ close (fd );
41+
42+ if ((fd = open (path ,O_RDWR ))< 0 ){
43+ perror ("open file failed" );
44+ return 1 ;
45+ }
46+ else
47+ {
48+ if ((n = read (fd ,buf ,40 )< 0 ))
49+ {
50+ perror ("open file 2 failed" );
51+ return 1 ;
52+ }
53+ else
54+ {
55+ printf ("read the changed data:\n" );
56+ printf ("%s\n" ,buf );
57+ }
58+ if (close (fd )< 0 ){
59+ perror ("close file failed" );
60+ return 1 ;
61+ }
62+ else
63+ {
64+ printf ("goodbye\n" );
65+ }
66+ }
67+ return 0 ;
68+ }
You can’t perform that action at this time.
0 commit comments