Linux下C编程学习笔记1

文件基础操作

打开和关闭文件

#inlcude <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);  //打开一个现有文件
int open(const char *pathname, int flags, mode_t mode); 
  1. pathname 是一个指针变量,传递包含了路径的完整文件名称
  2. flags参数说明
    | 只读 | O_RDONLY |0
    | 只写 | O_WRONLY |1
    | 读写 | O_RDWR | 2
    flags 可选参数
    O_CREAT(这里需要注意,有的书上写的是O_CREATE,会导致错误,正确写法没有E),文件不存在时,创建文件,此时会用到mode参数。
    O_APPEND, 文件已追加方式打开,每次进行写操作时,文件指针都会被放到文件末尾。
  3. mode 参数说明
    | S_IRWXU | 00700 | 文件属主有读、写、执行的权利
    | S_IRUSR(S_IREAD) | 00400 | 文件属主有读权利
    | S_IWUSR(S_IWRITE) | 00200 | 文件属主有写的权利
    | S_XWUSR(S_IEXEC) | 00100 | 文件属主有写的权利
  4. 应用实例
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
	int fd, temp;
	if(argc != 2)
	{	
		printf("Plz input the correct file name as './testOpen filename\n'");
	}
	esle
	{
		fd = open(*(argv+1), O_RDWR|O_CREAT, S_IRWXU);
		temp = close(fd);
	}
	exit(0);
}

——————————————————————————————————————————————————————

将数据写入文件

  1. 将待写入的字符直接放进缓冲字符串中写入,不需要从键盘接收字符;
  2. 使用gets函数并且写入末尾加入换行符
#include <stdlib.h>
#inclde <stdio.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
	int fd, temp;
	char writebuf[30];
	char endbuf[] = "\n";
	if(argc != 2)
	{
		printf("Plz input the correct file name as './testWrite filename\n'");
		return 1;
	}else
	{
		fd = open(*(argv+1), O_RDWR|O_CREAT, S_IREXU);
		//打开文件,如果没有就创建。
	}
	printf("The file descriptor is %d\n",fd);
	printf("Plz input the strings!\n");
	gets(writebuf);
	strcat(writebuf, endbuf);
	temp = write(fd, writebuf, strlen(writebuf));
	printf("The input string length is %d\n", temp);
	close(fd);
	exit(0);
}

——————————————————————————————————————————————————————

文件中进行定位操作

  1. lseek标准调用格式
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fds, off_t offset, int whence);

off_t 在linux中通常是long类型,其默认为一个32为整数, 在64位Linux系统中则会被编译为long long int,64位整数

  1. 参数说明
    offset文件偏移量,指的是每一次对文件的读写操作所需移动的距离,单位为字节,可正可负,正指前移,负指后移。
    whence有三种不同取值,SEEK_SET,设置偏移量为文件开始之后的offset个字节;SEEK_CUR,设置偏移量为当前偏移量之后的offset个字节;SEEK_END,设置偏移量为当前文件长度加上的offset个字节。

lseek函数允许文件的额偏移量被设置到超过文件结束符EOF处,然后再下一次调用write时,可以将文件的长度延伸到所需的长度,并用无意义的字符填充这个空隙,若是读到这段数据,将得到无意义值,知道这个文件数据块被真正的写在磁盘上,在读取这个空隙的数据时将得到0.

可以用下面的方式确定一个打开文件的偏移量
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);	//offset = 0,偏移量为0;
  1. 应用实例
#include <stdlib.h>
#inclde <stdio.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
	int temp, seektemp, i, j;
	int fd;
	char writebuf[30];
	char endbuf[] = "\r\n";
	if(argc != 2)
	{
		printf("Plz input the correct file name as './testWrite filename\n'");
		return 1;
	}else
	{
		fd = open(*(argv+1), O_RDWR|O_CREAT, S_IREXU);
		//打开文件,如果没有就创建。
	}
	printf("The file descriptor is %d\n",fd);
	printf("Plz input the strings!\n");
	gets(writebuf);
	strcat(writebuf, endbuf);
	temp = write(fd, writebuf, strlen(writebuf));

	seektemp = lseek(fd, 0, SEEK_CUR);	//获得当前的偏移量
	for( i = 0; i  < 10; i++)
	{
		printf("%d, Plz input the string and use Enter as the end\n", i);
		gets(writebuf);
		strcat(writebuf, endbuf);
		j = sizeof(writebuf)*(i+1);	//计算下一次的偏移量
		seektemp = lseek(fd, j , SEEK_SET);
		temp = write(fd, writebuf, strlen(writebuf));
	}
	close(fd);
	exit(0);
}

使用cat -n filename,即可查看输入的文件内容。
——————————————————————————————————————————————————————

从文件读出数据

  1. 标准调用格式
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);

通常来说,read函数的读操作是从文件的当前位移量出开始,在成功返回之前,该位移量增加到实际读到的字节数,但是有如下几种情况会使得实际独到的字节数少于要求读的字节数

  • 在读到要求字节数之前已经达到了文件尾端
  • 当从终端设备读时,通常一次最多读一行
  • 从网络读时,网络中的缓冲机构可能造成返回值小于所要求读的字节数
  • 某些面向记录的设备,如磁带,一次最多返回一个记录

32位的Linux中size_t 为unsigned int类型,64位的Linux为unsigned long

  1. 参数说明
  • buf,用于存放读出数据的缓冲区指针
  • count, 待读取 的数据长度,如果count为0,则read函数返回0并且没有其他结果,如果count大于32767,则结果不能确定
  1. 应用实例
    打开1文件,从其中读取一段数据写入2文件中
#include <fcntl.h>
#incldue <unistd.h>
#include <stdio.h>
#define PREMS 0666
#define DUMMY 0
#define MAXSIZE 1024
int main(int argc, char *argv[])
{
	int soursefd, targetfd;
	int readCounter = 0;	//读出的字符计数器
	char WRBuf[MAXSIZE];
	if(argc != 3)
	{
		printf("Plz input the correct file name as './testReadWrite filename1 filename2\n'");
		return 1;
	}
	
	if((sourcefd = open(*(argv+1), O_RDONLY, DUMMY)) == -1 )		//如果源文件打开失败
	{
		printf("Source file open error!\n");
		return 2;
	}

	if((targetfd = open(*(argv+2), O_WRONLY|O_CREAT , PERMS)) == -1 )		//如果源文件打开失败
	{
		printf("targetfd file open error!\n");
		return 3;
	}

	while((readCounter == read(sourcefd, WRBuf, MAXSIZE)) > 0)
	{
		if(write(targetfd, WRBuf, readCounter) != readCounter)
		{
			printf("Target file write error!\n");
			return 4;
		}
	}

	close(sourcefd);
	close(targetfd);
	exit(0);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值