linux 读写文件函数,Linux平台下利用系统接口函数按照行读写文件(示例代码)

要求:支持大文件(1M)一次性读入

源代码如下:

#include

#include

#include

#include

#define rwmode 2

//清屏命令函数

void clear()

{

char clscode[] = {0x1B, 0x5B, 0x48, 0x1B, 0x5B, 0x4A};

printf("%s",clscode);

}

//打开文件

int openFile()

{

int fd;

fd=open("/root/wcy/test1/test.txt",rwmode);

if(fd==-1){

printf("打开文件失败,文件路径不正确或者文件不存在!\n");

exit(0);

}else return fd;

}

//读取文件

void readFile(int fd,char buffer[],int len){

int n=read(fd,buffer,len);

buffer[n]=‘\0‘;

printf("文件的内容是:");

printf("%s",buffer);

printf("\n");

}

void lseekFile(int fd){

if(lseek(fd,0L,SEEK_END)==-1){

clear();

printf("定位读写文件失败!");

}

else{

char block[512]=" lseek file";

write(fd,block,strlen(block));

printf("定位读写文件成功!\n");

}

}

int main(){

int fd,n,select;

char buffer[1024*1024];

clear();

while(1){

printf("******************************\n") ;

printf("****** 读写文件系统 ******\n");

printf("**** 1 显示特定文件内容:****\n");

printf("**** 2 定位读写文件内容:****\n");

printf("**** 0 退出本系统 *****\n");

printf("******************************\n");

printf("请输入功能编号:");

scanf("%d",&select);

switch(select){

case 0:

clear();

close(fd);

exit(0);

case 1:

clear();

fd=openFile();

readFile(fd,buffer,sizeof(buffer)-1);

close(fd);

break;

case 2:

clear();

fd=openFile();

lseekFile(fd);

close(fd);

break;

default:

clear();

printf("你输入功能编号错误,请重新输入!\n");

}

}

exit(0);

}

注意:运行此程序的时候,务必有文件"/root/wcy/test1/test.txt",如果没有这个目录下的这个文件,请修改成你的文件所在的目录。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux平台下,可以使用系统接口函数来按读写文件支持文件定位读写。以下是一个示例代码,演示了如何使用系统调用函数来实现读写和定位读写: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> // 按读取文件 void read_line(int fd, off_t start_pos, int line_num) { off_t cur_pos = lseek(fd, start_pos, SEEK_SET); if (cur_pos == -1) { perror("lseek"); exit(EXIT_FAILURE); } char ch; int line_count = 0; while (read(fd, &ch, 1) > 0 && line_count < line_num) { if (ch == '\n') { line_count++; } putchar(ch); } } // 按写入文件 void write_line(int fd, off_t start_pos, int line_num, const char* content) { off_t cur_pos = lseek(fd, start_pos, SEEK_SET); if (cur_pos == -1) { perror("lseek"); exit(EXIT_FAILURE); } char ch; int line_count = 0; while (read(fd, &ch, 1) > 0 && line_count < line_num) { if (ch == '\n') { line_count++; } } if (write(fd, content, strlen(content)) == -1) { perror("write"); exit(EXIT_FAILURE); } } int main() { const char* filename = "example.txt"; int fd = open(filename, O_RDWR); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } // 按读取文件,从第3开始读取 read_line(fd, 0, 3); // 按写入文件,将第5的内容替换为"New line 5" write_line(fd, 0, 5, "New line 5"); close(fd); return 0; } ``` 请注意,上述代码中的文件定位是通过`lseek`函数实现的,`lseek`函数的第二个参数为文件中的偏移量,可以用于控制读写位置。`read`函数用于从文件中读取一个字符,`write`函数用于将内容写入文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值