笔记整理--Linux编程

时间:2023-12-05 19:41:02

linux c编程open() read() write()函数的使用方法及实例 | 奶牛博客 - Google Chrome (2013/8/31 17:56:10)

今天把文件IO操作的一些东东整理下.基本的,对于锁机制下次再整理.常用的文件IO函数有标题的三个open() read() write() .首先打开一个文件使用open()函数,然后可以获取到一个文件描述符,这个就是程序中调用这个打开文件的一个链接,当函数要求到文件描述符fd的时候就把这个返回值给函数即可.read跟write都差不多,格式:read(文件描述符,参数,权限) write(文件描述符,参数,权限),返回值是读取或写入的字符数.其中的权限可以省略,文件描述符就是open()函数的返回值,而参数呢有O_RDONLY(只读) O_WRONLY(只写) O_RDWR(读写) O_CREAT(若不存在则新建) O_TRUNC(若不为空则清空文件)等.对函数想有更多了解可以察看linux下c编程的函数手册.

有些程序还是得自己动手写一下,自己不写就很难知道问题错在哪里,不知道自己是不是真的可以写出来.

写个小例子:文件备份程序

功能:输入一个"文件名",将生成"文件名.backup"的备份文件.若输入量超过两个或者无输入量,报错,若文件不存在或无权限,报错.成功返回提示.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. int main(int argc,char *args[]) {
  6. char buff[1024];
  7. int fd1,fd2,i;
  8. int baksize = sizeof(args[1])+7;
  9. char bakfile[baksize];
  10. // input one file only
  11. if(argc != 2){
  12. printf("Input one file a time!\n");
  13. exit(1);
  14. }
  15. //bakfile="XXX.backup"设置备份文件名称
  16. strcpy(bakfile,args[1]);
  17. strcat(bakfile,".backup");
  18. //open()
  19. fd1 = open(args[1],O_RDONLY,0644);
  20. fd2 = open(bakfile,O_RDWR|O_CREAT|O_TRUNC);
  21. if((fd1 < 0)||(fd2 < 0)){
  22. printf("Open Error!Check if the file is exist and you have the permission!\n");
  23. exit(1);
  24. }
  25. //read from fd1 and write buff to fd2
  26. while((i = read(fd1,buff,sizeof(buff))) > 0) {
  27. write(fd2,buff,i);//这里需要用读取到的字符数,否则会出错,因为buff数组有可能未被全覆盖
  28. }
  29. close(fd1);
  30. close(fd2);
  31. printf("Backup done!\n");
  32. exit(0);
  33. }

Linux编程 sockaddr_in sockaddr in_addr详解。 - 技术家园 - ITeye技术网站 - Google Chrome (2013/7/1 22:24:43)

sockaddr_in结构体:

  1. struct sockaddr_in {
  2. short int sin_family; // Address family
  3. unsigned short int sin_port; // Port number
  4. struct in_addr sin_addr; // Internet address
  5. unsigned char sin_zero[8]; // Same size as struct sockaddr
  6. };

sockaddr结构体:

  1. struct sockaddr {
  2. unsigned short sa_family; // address family, AF_xxx
  3. char sa_data[14]; // 14 bytes of protocol address
  4. };

in_addr结构体:

  1. struct in_addr {
  2. unsigned long s_addr; // that’s a 32-bit long, or 4 bytes
  3. };

在填充sockaddr_in结构体时:serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
sockaddr_in与sockaddr都是16个字节,只是sockaddr的表现形式更形象,对象化而已。而in_addr结构体就是一个long型

可以猜测h_addr_list是一个4字节网络字节序的in_addr,只是使用char*来表示,h_addr与struct in_addr其实形式是一样的。然后将host->h_addr强转成struct in_addr格式的指针,最后取值。(哈哈,这个就是C比Java强悍的地方,指针可真是一把双面刃).

Linux下C编程,进程通信之无名管道通信 - 旭东的博客 - 博客园 - Google Chrome (2013/3/29 9:59:20)

Linux下C编程,进程通信之无名管道通信

最近在看进程间的通信,下面说说管道通信之无名管道。

1.概述

  管道是Linux中很重要的一种通信方式,他是把一个程序的输出直接连接到另一个程序的输入,并且管道具有队列的特性。如Linux命令,“ps -ef | grep root”。如下图所示:

笔记整理--Linux编程

2.无名管道

  2.1特点

  (1)它只能用于具有亲缘关系的进程之间的通信(也就是父子进程或者兄弟进程之间)。
  (2)它是一个半双工的通信模式,具有固定的读端和写端。
  (3)管道也可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。

  2.2主要函数说明

  int pipe(int fd[2])

    传入参数fd[2]数组,管道的两个文件描述符,之后就可以直接操作这两个文件描述符。其中fd[0]是“读”描述符,fd[1]是“写”描述符。

  2.3使用代码及说明

笔记整理--Linux编程
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pipe_fd[2];  /*用于保存两个文件描述符*/
pid_t pid;
char buf_r[100];  /*用于读数据的缓存*/
int r_num;  /*用于保存读入数据大数量*/
memset(buf_r,0,sizeof(buf_r)); if(pipe(pipe_fd)<0) /*创建管道,成功返回0,否则返回-1*/
return -1; /*fork()创建一子进程,
   具体使用可以参见:http://www.cnblogs.com/xudong-bupt/archive/2013/03/26/2982029.html*/
if((pid=fork())==0)
{
close(pipe_fd[1]); /*关闭子进程写描述符,并通过使父进程暂停 2 秒确保父进程已关闭相应的读描述符*/
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0) /*子进程读取管道内容*/
printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
close(pipe_fd[0]);/*关闭子进程读描述符*/
exit(0);
} else if(pid>0)
{
close(pipe_fd[0]);/*/关闭父进程读描述符,并分两次向管道中写入 Hello Pipe*/
if(write(pipe_fd[1],"Hello",5)!= -1)
printf("parent write1 success!\n");
if(write(pipe_fd[1]," Pipe",5)!= -1)
printf("parent write2 success!\n");
close(pipe_fd[1]);/*关闭父进程写描述符*/
sleep(3);
exit(0);
}
}
笔记整理--Linux编程

SHELL (2013/2/26 16:34:41)

笔记整理--Linux编程 笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程笔记整理--Linux编程