linux C++ 读写文件

35 篇文章 0 订阅

从现在开始,进行linux的学习了,开始呢,总想着,windows下的C++已经很熟悉了,所以呢,linux'下的c编程,应该就很快上手,上来就不应该从0开始了,可渐渐发现,根本不是这样的,即使是一个简单的文件读写操作,也使用了很长时间,呵呵,眼高手低阿,不过,还是很有收获的,慢慢来吧,还有时间,学习!学习

恩就这样吧,

#include<cstring>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
#include<iostream>
using namespace std;
int main()
{
   //open the file if not process exit and output the err message
   char* fileName="hehe.txt";
   int fileDes=open(fileName,O_CREAT|O_RDWR|O_APPEND,0666);// we need the file fcntl.h
   if(fileDes==-1)
   {
      cout<<"the file open failed with the code : "<<errno<<endl; //we need the errno.h
      return 0;
   }
   cout<<"the file open success\n";
   //first we need to read the file content and then output to the screen after that write the time
   int fileSize=lseek(fileDes,0,SEEK_END)+1;
   lseek(fileDes,0,fileSize);
    char buf[1024]={0};

   while(read(fileDes,buf,1024)>0)//read we need the file unistd.h
   {
      cout<<buf<<endl;
   }
   strcpy(buf,"\nthis is a other day\n");
   if(write(fileDes,buf,strlen(buf))==-1)// write we need the file unistd.h strlen need the file cstring
   {
      cout<<"write file error with the code :"<<errno<<endl; 
   }
   close(fileDes);//close we need the file unistd.h
}


这里面需要注意的是,如果没有文件则建立新的文件,就要使用O_CREAT,但是呢,仅仅使用这个参数,只是确定了文件的新建操作,而读写还要设定O_RDWR.可读可写才行,否则会出现write错误 errno代码是9,

再有获得文件大小是(fileDes,0,SEEK_END)+1,这样就获得了文件的大小,再使用lseek移动文件指针即可,向里面写,即可

文件嵌入资源文件后,可以使用相应的函数读取资源文件中的数据。在Linux中,可以使用以下两种方式来读取资源文件: 1. 使用C标准库函数 可以使用fopen、fread、fclose等C标准库函数来读取资源文件。例如: ```cpp #include <stdio.h> int main() { // 打开资源文件 FILE *fp = fopen("/path/to/resource", "rb"); if (fp == NULL) { printf("Failed to open resource file\n"); return -1; } // 定位文件读写位置 fseek(fp, 0, SEEK_SET); // 读取文件数据 char buffer[1024]; int count = fread(buffer, 1, sizeof(buffer), fp); if (count < 0) { printf("Failed to read resource file\n"); return -1; } // 输出读取的数据 printf("Read %d bytes from resource file:\n", count); printf("%s\n", buffer); // 关闭资源文件 fclose(fp); return 0; } ``` 2. 使用系统调用函数 可以使用open、read、close等系统调用函数来读取资源文件。例如: ```cpp #include <iostream> #include <fcntl.h> #include <unistd.h> using namespace std; int main() { // 打开资源文件 int fd = open("/path/to/resource", O_RDONLY); if (fd == -1) { cout << "Failed to open resource file" << endl; return -1; } // 定位文件读写位置 lseek(fd, 0, SEEK_SET); // 读取文件数据 char buffer[1024]; int count = read(fd, buffer, sizeof(buffer)); if (count < 0) { cout << "Failed to read resource file" << endl; return -1; } // 输出读取的数据 cout << "Read " << count << " bytes from resource file:" << endl; cout << buffer << endl; // 关闭资源文件 close(fd); return 0; } ``` 需要注意的是,读取资源文件时需要使用正确的路径和文件名。另外,在使用系统调用函数时需要注意文件的权限问题,对于某些系统级别的资源文件,可能需要具有管理员权限才能访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值