设为首页 收藏本站
查看: 943|回复: 0

[经验分享] Linux系统开发 2 文件IO open() close() read() write() perror() lseek() fcntl() ioc

[复制链接]
发表于 2019-1-27 13:45:24 | 显示全部楼层 |阅读模式
本文谢绝转载,原文来自http://990487026.blog.运维网.com



大纲
Linux系统开发
man 文档的使用
文件IO
open() 创建文件,指定权限位
open() 接收参数 创建文件
open() 传两个参数 第三个参数从内存取垃圾值
write()函数 向文件写数据
write()函数的覆盖操作
open()函数文件的追加
open() 创建文件,如果文件已经存在,就报错
测试一个程序最多能创建1021个文件,3个STDIN STDOUT STDERR已经存在了
通过ulimit命令也可以直接查询最多可以创建多少文件
修改ulimit -n 改变最大创建文件个数
最大最大能打开多少文件,取决于系统,据说是根据内存大小有关系
read write 文件复制(非二进制)
阻塞演示:输入输出必须等待回应才能执行下一步
非阻塞程序:
perror 报错函数
打印出错误的原因
Linux所有错误代码
文件lseek操作
lseek获取文件的大小
fcntl获取或者设置文件的访问控制属性
fcntl实例,以阻塞模式打开stdin,设置为非阻塞模式
ioctl 获取终端的行高 与 列宽
ioctl获取分辨率
习题  

  

  

  man page的使用
1、Standard commands (标准命令)
2、System calls (系统调用)
3、Library functions (库函数)
4、Special devices (设备说明)
5、File formats (文件格式)
6、Games and toys (游戏和娱乐)
7、Miscellaneous (杂项)
8、Administrative Commands (管理员命令)
9 其他(Linux特定的), 用来存放内核例行程序的文档。
2016-08-01_20:02:34chunli@http://990487026.blog.运维网.com~$ man 3 printf
2016-08-01_20:02:34chunli@http://990487026.blog.运维网.com~$ man 2 open  

  linux API函数 open函数,创建文件,指定权限位
2016-08-01_17:48:54root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
//linux apiman 2 open
#include
#include
#include
int main(void)
{
int ret = open("abc",O_CREAT,0777);//设置权限位为777
printf("%d\n",ret);
return 0;
}
编译运行:
2016-08-01_17:47:40root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app
3
可以看到abc
2016-08-01_17:49:17root@http://990487026.blog.运维网.com~/linux_c# ll
total 16K
-rwxr-xr-x 1 root   root      0 Aug  1 17:47 abc*
-rwxr-xr-x 1 root   root   8.5K Aug  1 17:48 app*
-rw-rw-r-- 1 chunli chunli  215 Aug  1 17:48 main.c
查看系统的umask
2016-08-01_17:50:00root@http://990487026.blog.运维网.com~/linux_c# umask
0022
为什么是755?
0777 - 0022 == 0755
我把umask搞一下
2016-08-01_17:51:06root@http://990487026.blog.运维网.com~/linux_c# umask 0
2016-08-01_17:52:33root@http://990487026.blog.运维网.com~/linux_c# rm abc
2016-08-01_17:52:40root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app
3
2016-08-01_17:52:42root@http://990487026.blog.运维网.com~/linux_c# ll
total 16K
-rwxrwxrwx 1 root   root      0 Aug  1 17:52 abc*
-rwxrwxrwx 1 root   root   8.5K Aug  1 17:52 app*
-rw-rw-r-- 1 chunli chunli  239 Aug  1 17:51 main.c  

  main函数接收参数,调用open函数创建文件
2016-08-01_18:23:50root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
//linux apiman 2 open
#include
#include
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
int ret = open(args[1],O_CREAT,0777);//设置权限位为777
printf("%d\n",ret);
return 0;
}
2016-08-01_18:23:55root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
3
2016-08-01_18:24:03root@http://990487026.blog.运维网.com~/linux_c# ll
total 16K
-rwxr-xr-x 1 root   root      0 Aug  1 18:24 123*
-rwxr-xr-x 1 root   root   8.5K Aug  1 18:24 app*
-rw-rw-r-- 1 chunli chunli  406 Aug  1 18:23 main.c
2016-08-01_18:24:06root@http://990487026.blog.运维网.com~/linux_c# umask
0022
2016-08-01_18:24:10root@http://990487026.blog.运维网.com~/linux_c#  

  两个参数的open函数,第三个参数从内存取垃圾值
2016-08-01_18:25:19root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
//linux apiman 2 open
#include
#include
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
int ret = open(args[1],O_CREAT);//设置权限位为777
printf("%d\n",ret);
return 0;
}
编译运行:
2016-08-01_18:24:55root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
3
权限位很奇怪
2016-08-01_18:24:57root@http://990487026.blog.运维网.com~/linux_c# ll
---x--S--- 1 root   root      0 Aug  1 18:24 123*
-rwxr-xr-x 1 root   root   8.5K Aug  1 18:24 app*
-rw-rw-r-- 1 chunli chunli  401 Aug  1 18:24 main.c
2016-08-01_18:25:20root@http://990487026.blog.运维网.com~/linux_c# umask
0022  
  write()函数 向文件写数据
2016-08-01_19:35:52root@http://990487026.blog.运维网.com~/linux_c# rm 123  app
2016-08-01_19:35:56root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
//open函数返回文件描述符 0 1 2 =>STDIN STDOUT STDERR
int fd = open(args[1],O_CREAT|O_RDWR,0644);//创建文件可读可写
char buf[] = "Hello World!\n";
write(fd,buf,strlen(buf));//写到当前进程文件描述符为fd的文件
printf("%d\n",fd);
return 0;
}
2016-08-01_19:35:58root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
3
2016-08-01_19:36:01root@http://990487026.blog.运维网.com~/linux_c# cat 123
Hello World!
2016-08-01_19:36:08root@http://990487026.blog.运维网.com~/linux_c#  

  write()函数的覆盖操作 O_RDWR ,当open函数不需要create的时候,就不需要第3个函数
2016-08-01_19:40:53root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
//open函数返回文件描述符 0 1 2 =>STDIN STDOUT STDERR
//int fd = open(args[1],O_CREAT|O_RDWR,0644);//创建文件可读可写
int fd = open(args[1],O_RDWR);//创建文件可读可写
char buf[] = "World!\n";
write(fd,buf,strlen(buf));//写到当前进程文件描述符为fd的文件
printf("%d\n",fd);
return 0;
}

2016-08-01_19:39:27root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
3
2016-08-01_19:40:16root@http://990487026.blog.运维网.com~/linux_c# cat 123
World!
orld!
2016-08-01_19:40:19root@http://990487026.blog.运维网.com~/linux_c#  

  open()函数文件的追加 _RDWR|O_APPEND
2016-08-01_19:42:36root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
//open函数返回文件描述符 0 1 2 =>STDIN STDOUT STDERR
//int fd = open(args[1],O_CREAT|O_RDWR,0644);//创建文件可读可写
int fd = open(args[1],O_RDWR|O_APPEND,0644);//创建文件可读可写
//int fd = open(args[1],O_RDWR);//创建文件可读可写
char buf[] = "Linux haha !\n";
write(fd,buf,strlen(buf));//写到当前进程文件描述符为fd的文件
printf("%d\n",fd);
return 0;
}
2016-08-01_19:42:37root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
3
2016-08-01_19:42:40root@http://990487026.blog.运维网.com~/linux_c# cat 123
World!
orld!
Linux haha !
2016-08-01_19:42:43root@http://990487026.blog.运维网.com~/linux_c#  

  open(args[1],O_CREAT | O_RDWR | O_EXCL,0644) 创建文件,如果文件已经存在,就报错
2016-08-01_19:49:44root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
if(argc < 2)
{
printf("请输入需要被创建的文件名\n");
exit(1);//任何函数调用exit,程序直接退出
}
int fd = open(args[1],O_CREAT | O_RDWR | O_EXCL,0644);//O_EXCL 如果文件已经存在,直接报错
char buf[] = "Linux haha !\n";
write(fd,buf,strlen(buf));//写到当前进程文件描述符为fd的文件
printf("%d\n",fd);
return 0;
}
2016-08-01_19:49:45root@http://990487026.blog.运维网.com~/linux_c# ll
total 20K
-rw-r--r-- 1 root   root     26 Aug  1 19:42 123
-rwxr-xr-x 1 root   root   8.7K Aug  1 19:49 app*
-rw-rw-r-- 1 chunli chunli  705 Aug  1 19:49 main.c
2016-08-01_19:49:46root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
-1
2016-08-01_19:49:48root@http://990487026.blog.运维网.com~/linux_c#  

  测试一个程序最多能创建多少个文件,1021个,其他3个STDIN STDOUT STDERR已经存在了
2016-08-01_20:24:55root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
//if(argc < 2)
//{
//printf("请输入需要被创建的文件名\n");
//exit(1);//任何函数调用exit,程序直接退出
//}
char buf[10];
int i = 0;
while(1)
{
sprintf(buf,"file_%d",++i);
int fd = open(buf,O_CREAT,0644);//O_EXCL 如果文件已经存在,直接报错
if(fd == -1)
{
break;
}
else
{
printf("%d\t",i);
if(i % 10 == 0)
printf("\n");
}
}
printf("\n");
return 0;
}
2016-08-01_20:24:56root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
12345678910
11121314151617181920
21222324252627282930
31323334353637383940
41424344454647484950
51525354555657585960
61626364656667686970
71727374757677787980
81828384858687888990
919293949596979899100
101102103104105106107108109110
111112113114115116117118119120
121122123124125126127128129130
131132133134135136137138139140
141142143144145146147148149150
151152153154155156157158159160
161162163164165166167168169170
171172173174175176177178179180
181182183184185186187188189190
191192193194195196197198199200
201202203204205206207208209210
211212213214215216217218219220
221222223224225226227228229230
231232233234235236237238239240
241242243244245246247248249250
251252253254255256257258259260
261262263264265266267268269270
271272273274275276277278279280
281282283284285286287288289290
291292293294295296297298299300
301302303304305306307308309310
311312313314315316317318319320
321322323324325326327328329330
331332333334335336337338339340
341342343344345346347348349350
351352353354355356357358359360
361362363364365366367368369370
371372373374375376377378379380
381382383384385386387388389390
391392393394395396397398399400
401402403404405406407408409410
411412413414415416417418419420
421422423424425426427428429430
431432433434435436437438439440
441442443444445446447448449450
451452453454455456457458459460
461462463464465466467468469470
471472473474475476477478479480
481482483484485486487488489490
491492493494495496497498499500
501502503504505506507508509510
511512513514515516517518519520
521522523524525526527528529530
531532533534535536537538539540
541542543544545546547548549550
551552553554555556557558559560
561562563564565566567568569570
571572573574575576577578579580
581582583584585586587588589590
591592593594595596597598599600
601602603604605606607608609610
611612613614615616617618619620
621622623624625626627628629630
631632633634635636637638639640
641642643644645646647648649650
651652653654655656657658659660
661662663664665666667668669670
671672673674675676677678679680
681682683684685686687688689690
691692693694695696697698699700
701702703704705706707708709710
711712713714715716717718719720
721722723724725726727728729730
731732733734735736737738739740
741742743744745746747748749750
751752753754755756757758759760
761762763764765766767768769770
771772773774775776777778779780
781782783784785786787788789790
791792793794795796797798799800
801802803804805806807808809810
811812813814815816817818819820
821822823824825826827828829830
831832833834835836837838839840
841842843844845846847848849850
851852853854855856857858859860
861862863864865866867868869870
871872873874875876877878879880
881882883884885886887888889890
891892893894895896897898899900
901902903904905906907908909910
911912913914915916917918919920
921922923924925926927928929930
931932933934935936937938939940
941942943944945946947948949950
951952953954955956957958959960
961962963964965966967968969970
971972973974975976977978979980
981982983984985986987988989990
9919929939949959969979989991000
1001100210031004100510061007100810091010
1011101210131014101510161017101810191020
1021
2016-08-01_20:24:58root@http://990487026.blog.运维网.com~/linux_c#  

  

  通过ulimit命令也可以直接查询最多可以创建多少文件
2016-08-01_20:26:24root@http://990487026.blog.运维网.com~/linux_c# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3823
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3823
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
2016-08-01_20:26:27root@http://990487026.blog.运维网.com~/linux_c#  

  修改ulimit -n 改变最大创建文件个数
2016-08-01_20:28:23root@http://990487026.blog.运维网.com~/linux_c# cat main.c
//C标准头文件
#include
#include
#include
//linux apiman 2 open
//open() and creat() return the new file descriptor, or -1 if an error occurred
#include
#include
#include
//linux api     man 2 close
#include
int main(int argc,char **args)
{
//if(argc < 2)
//{
//printf("请输入需要被创建的文件名\n");
//exit(1);//任何函数调用exit,程序直接退出
//}
char buf[10];
int i = 0;
while(1)
{
sprintf(buf,"file_%d",++i);
int fd = open(buf,O_CREAT,0644);//O_EXCL 如果文件已经存在,直接报错
if(fd == -1)
{
break;
}
else
{
printf("%d\t",i);
if(i % 20 == 0)
printf("\n");
}
}
printf("\n");
return 0;
}
2016-08-01_20:28:26root@http://990487026.blog.运维网.com~/linux_c# ulimit -n 2000
2016-08-01_20:29:34root@http://990487026.blog.运维网.com~/linux_c# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3823
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 2000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3823
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
2016-08-01_20:29:38root@http://990487026.blog.运维网.com~/linux_c#
2016-08-01_20:28:27root@http://990487026.blog.运维网.com~/linux_c# gcc -o app main.c  && ./app  123
1234567891011121314151617181920
2122232425262728293031323334353637383940
4142434445464748495051525354555657585960
6162636465666768697071727374757677787980
81828384858687888990919293949596979899100
101102103104105106107108109110111112113114115116117118119120
121122123124125126127128129130131132133134135136137138139140
141142143144145146147148149150151152153154155156157158159160
161162163164165166167168169170171172173174175176177178179180
181182183184185186187188189190191192193194195196197198199200
201202203204205206207208209210211212213214215216217218219220
221222223224225226227228229230231232233234235236237238239240
241242243244245246247248249250251252253254255256257258259260
261262263264265266267268269270271272273274275276277278279280
281282283284285286287288289290291292293294295296297298299300
301302303304305306307308309310311312313314315316317318319320
321322323324325326327328329330331332333334335336337338339340
341342343344345346347348349350351352353354355356357358359360
361362363364365366367368369370371372373374375376377378379380
381382383384385386387388389390391392393394395396397398399400
401402403404405406407408409410411412413414415416417418419420
421422423424425426427428429430431432433434435436437438439440
441442443444445446447448449450451452453454455456457458459460
461462463464465466467468469470471472473474475476477478479480
481482483484485486487488489490491492493494495496497498499500
501502503504505506507508509510511512513514515516517518519520
521522523524525526527528529530531532533534535536537538539540
541542543544545546547548549550551552553554555556557558559560
561562563564565566567568569570571572573574575576577578579580
581582583584585586587588589590591592593594595596597598599600
601602603604605606607608609610611612613614615616617618619620
621622623624625626627628629630631632633634635636637638639640
641642643644645646647648649650651652653654655656657658659660
661662663664665666667668669670671672673674675676677678679680
681682683684685686687688689690691692693694695696697698699700
701702703704705706707708709710711712713714715716717718719720
721722723724725726727728729730731732733734735736737738739740
741742743744745746747748749750751752753754755756757758759760
761762763764765766767768769770771772773774775776777778779780
781782783784785786787788789790791792793794795796797798799800
801802803804805806807808809810811812813814815816817818819820
821822823824825826827828829830831832833834835836837838839840
841842843844845846847848849850851852853854855856857858859860
861862863864865866867868869870871872873874875876877878879880
881882883884885886887888889890891892893894895896897898899900
901902903904905906907908909910911912913914915916917918919920
921922923924925926927928929930931932933934935936937938939940
941942943944945946947948949950951952953954955956957958959960
961962963964965966967968969970971972973974975976977978979980
9819829839849859869879889899909919929939949959969979989991000
10011002100310041005100610071008100910101011101210131014101510161017101810191020
10211022102310241025102610271028102910301031103210331034103510361037103810391040
10411042104310441045104610471048104910501051105210531054105510561057105810591060
10611062106310641065106610671068106910701071107210731074107510761077107810791080
10811082108310841085108610871088108910901091109210931094109510961097109810991100
11011102110311041105110611071108110911101111111211131114111511161117111811191120
11211122112311241125112611271128112911301131113211331134113511361137113811391140
11411142114311441145114611471148114911501151115211531154115511561157115811591160
11611162116311641165116611671168116911701171117211731174117511761177117811791180
11811182118311841185118611871188118911901191119211931194119511961197119811991200
12011202120312041205120612071208120912101211121212131214121512161217121812191220
12211222122312241225122612271228122912301231123212331234123512361237123812391240
12411242124312441245124612471248124912501251125212531254125512561257125812591260
12611262126312641265126612671268126912701271127212731274127512761277127812791280
12811282128312841285128612871288128912901291129212931294129512961297129812991300
13011302130313041305130613071308130913101311131213131314131513161317131813191320
13211322132313241325132613271328132913301331133213331334133513361337133813391340
13411342134313441345134613471348134913501351135213531354135513561357135813591360
13611362136313641365136613671368136913701371137213731374137513761377137813791380
13811382138313841385138613871388138913901391139213931394139513961397139813991400
14011402140314041405140614071408140914101411141214131414141514161417141814191420
14211422142314241425142614271428142914301431143214331434143514361437143814391440
14411442144314441445144614471448144914501451145214531454145514561457145814591460
14611462146314641465146614671468146914701471147214731474147514761477147814791480
14811482148314841485148614871488148914901491149214931494149514961497149814991500
15011502150315041505150615071508150915101511151215131514151515161517151815191520
15211522152315241525152615271528152915301531153215331534153515361537153815391540
15411542154315441545154615471548154915501551155215531554155515561557155815591560
15611562156315641565156615671568156915701571157215731574157515761577157815791580
15811582158315841585158615871588158915901591159215931594159515961597159815991600
16011602160316041605160616071608160916101611161216131614161516161617161816191620
16211622162316241625162616271628162916301631163216331634163516361637163816391640
16411642164316441645164616471648164916501651165216531654165516561657165816591660
16611662166316641665166616671668166916701671167216731674167516761677167816791680
16811682168316841685168616871688168916901691169216931694169516961697169816991700
17011702170317041705170617071708170917101711171217131714171517161717171817191720
17211722172317241725172617271728172917301731173217331734173517361737173817391740
17411742174317441745174617471748174917501751175217531754175517561757175817591760
17611762176317641765176617671768176917701771177217731774177517761777177817791780
17811782178317841785178617871788178917901791179217931794179517961797179817991800
18011802180318041805180618071808180918101811181218131814181518161817181818191820
18211822182318241825182618271828182918301831183218331834183518361837183818391840
18411842184318441845184618471848184918501851185218531854185518561857185818591860
18611862186318641865186618671868186918701871187218731874187518761877187818791880
18811882188318841885188618871888188918901891189218931894189518961897189818991900
19011902190319041905190619071908190919101911191219131914191519161917191819191920
19211922192319241925192619271928192919301931193219331934193519361937193819391940
19411942194319441945194619471948194919501951195219531954195519561957195819591960
19611962196319641965196619671968196919701971197219731974197519761977197819791980
19811982198319841985198619871988198919901991199219931994199519961997
2016-08-01_20:28:30root@http://990487026.blog.运维网.com~/linux_c#  

  
  最大最大能打开多少文件,取决于系统,据说是根据内存大小有关系
2016-08-01_20:31:00root@http://990487026.blog.运维网.com~/linux_c# cat /proc/sys/fs/file-max
97372  

  read write 文件复制(非二进制)
2016-08-01_21:24:08chunli@http://990487026.blog.运维网.com~/linux_c$ cat main.c
//C标准头文件
#include
#include
#include
//linux api
#include
#include
#include
#include
#define SIZE 8192
int main(int argc,char **args)
{
if(argc < 3)
{
printf("参数不够\n");
exit(1);//任何函数调用exit,程序直接退出
}
char buf[SIZE] = {0};
int fd_src = 0;
int fd_dest = 0;
int len = 0;
fd_src  =open(args[1],O_RDONLY);
if(fd_src == -1)
{
printf("open(args[1],O_RDONLY) 文件打开失败!\n");
return fd_src;
}
fd_dest =open(args[2],O_CREAT | O_WRONLY | O_TRUNC,0755 );//创建文件,只写,截断
if(fd_dest == -1)
{
printf("open(args[2],O_CREAT | O_WRONLY | O_TRUNC,0755 ) 文件打开失败!\n");
return fd_dest;
}
//成功返回督导的字符个数
//读到末尾返回0
//读失败返回-1
while(len = read(fd_src,buf,sizeof(buf)) )
{
write(fd_dest,buf,len);
}
return 0;
}
编译运行
2016-08-01_21:24:11chunli@http://990487026.blog.运维网.com~/linux_c$ gcc main.c  -o app && ./app main.c  123
查看复制的效果怎么样
2016-08-01_21:24:13chunli@http://990487026.blog.运维网.com~/linux_c$ cat 123
//C标准头文件
#include
#include
#include
//linux api
#include
#include
#include
#include
#define SIZE 8192
int main(int argc,char **args)
{
if(argc < 3)
{
printf("参数不够\n");
exit(1);//任何函数调用exit,程序直接退出
}
char buf[SIZE] = {0};
int fd_src = 0;
int fd_dest = 0;
int len = 0;
fd_src  =open(args[1],O_RDONLY);
if(fd_src == -1)
{
printf("open(args[1],O_RDONLY) 文件打开失败!\n");
return fd_src;
}
fd_dest =open(args[2],O_CREAT | O_WRONLY | O_TRUNC,0755 );//创建文件,只写,截断
if(fd_dest == -1)
{
printf("open(args[2],O_CREAT | O_WRONLY | O_TRUNC,0755 ) 文件打开失败!\n");
return fd_dest;
}
//成功返回督导的字符个数
//读到末尾返回0
//读失败返回-1
while(len = read(fd_src,buf,sizeof(buf)) )
{
write(fd_dest,buf,len);
}
return 0;
}
2016-08-01_21:24:16chunli@http://990487026.blog.运维网.com~/linux_c$  

  
  阻塞演示:输入输出必须等待回应才能执行下一步
chunli@ubuntu:~/linux_c$ cat main.c
#include
#include
#include

int main(void)
{
int len  = 0;
char buf[30] = {0};
len = read(STDIN_FILENO,buf,sizeof(buf));
write(STDOUT_FILENO,buf,len);
return 0;
}
chunli@ubuntu:~/linux_c$ gcc main.c  && ./a.out
hahaha
hahaha
chunli@ubuntu:~/linux_c$  

  
  非阻塞程序:
chunli@ubuntu:~/linux_c$ cat main.c
#include
#include //exit
#include
#include
#include
#include
#include
#include //perror
#define MSG_TRY "try again \n"
int main(void)
{
char buf[10] = {0};
int fd = 0;
int n = 0;
fd = open("/dev/tty",O_RDONLY | O_NONBLOCK);
if(fd < 0)
{
perror("open /dev/tty");
exit(1);
}
tryagain:
n = read(fd,buf,sizeof(buf));
if(n < 0)
{
if(errno == EAGAIN)
{
sleep(3);
write(STDOUT_FILENO,MSG_TRY,strlen(MSG_TRY));
goto tryagain;
}
perror("read /dev/tty");
exit(2);
}
write(STDOUT_FILENO,buf,n);
close(fd);
return 0;
}
编译运行:
chunli@ubuntu:~/linux_c$ gcc main.c  && ./a.out
hello
try again
hello
chunli@ubuntu:~/linux_c$  

  
  tty
chunli@ubuntu:~$ tty
/dev/pts/0
chunli@ubuntu:~$ who
chunli   pts/0        2016-08-02 10:35 (10.11.12.1)
chunli   pts/5        2016-08-02 08:56 (10.11.12.1)
chunli@ubuntu:~$ sudo echo `date` >>  /dev/tty
Tue Aug 2 11:35:06 CST 2016
chunli@ubuntu:~$  

  perror 报错函数,全局变量errno,perrno去找这个数字,报出原因
chunli@ubuntu:~/linux_c$ cat main.c
#include
#include //exit
#include
#include
#include
#include
#include //perror
int main(void)
{
        int fd = open("123",O_WRONLY);
        if(fd

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668289-1-1.html 上篇帖子: extman登陆报错Can't open /tmp/extman/, No such file or directory 下篇帖子: android模拟器创建时的PANIC: Could not open:错误的解决
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表