Skip to content

Commit 2bee35f

Browse files
committed
new file: CMakeLists.txt
new file: include/head.h new file: src/main.c
1 parent ce35c6e commit 2bee35f

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

c/fd/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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})

c/fd/include/head.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
#include <sys/stat.h>
5+
#include <fcntl.h>
6+

c/fd/src/main.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

0 commit comments

Comments
 (0)