Linux文件读写之得到重复的内容


       在Linux系统中,我们经常需要对问文件进行操作,文件的读写时又经常会出现各种各样的问题。在这里我就讲一下我在进行文件读写操作时遇到的问题。


背景:首先向文件中写入内容,然后从文件中从后往前读取文件中的内容;

在Qt环境下的编程(代码中用红色标注的地方为重点内容)

代码如下:

#include "mainwidget.h"
#include "ui_mainwidget.h"

#include<stdio.h>

#include<string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define    FILENAME  "alamDetailList"     //文件名

#define  ARRAYSIZE 200

typedef struct alarmMessage{
    int msgSize;                           //数据大小
    char mesInfo[ARRAYSIZE];    //数据内容
}msg_t;


void mainWidget::saveMessageToFile()     //把内容写入文件中
{
    int isFileExist = access(FILENAME,F_OK);       //判断文件是否存在
    int fd;
    if (isFileExist == 0)   //文件存在
    {
        fd = open(FILENAME,O_WRONLY);
        qDebug() <<"打开文件";
    }else {
        qDebug()<<"创建并打开文件";
        fd = open(FILENAME,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
    }


    if(fd == -1)
    {
        qDebug() << tr("打开文件失败");
    }
   lseek(fd,0,SEEK_END);    //每次写入文件之前,都移到文件的最后的位置


   //alarmMSGDeatil 是QString类型,也就是要写入文件中的内容,下面是将QString类型转换为char *类型
   const char *detailTime = alarmMSGDeatil.toStdString().c_str();
   msg_t msg;
   memset(msg.mesInfo,'\0',sizeof(msg.mesInfo));
   msg.msgSize = strlen(detailTime);
   strcpy(msg.mesInfo,detailTime);
   int bytesWrite = write(fd,&msg,sizeof(msg));
   qDebug() << "==bytesWrite111=="<<bytesWrite;
   ::close(fd);
}

//读取文件内容,从后往前读

void mainWidget::readFile()
{
    qDebug("11111111111");
    //ui->listWidget->setVisible(true);
    int fd = open(FILENAME,O_RDONLY);
    if(fd == -1)
    {
        qDebug() << "打开文件失败";
        return;
    }
   lseek(fd, 0 ,SEEK_END);      //位置指向文件的最后


    off_t  offset = 0;
    int readBytes = 0;
    //ui->listWidget->clear();
     while(1)                           //通过死循环来完成文件中所有内容的读写
     {
         offset += sizeof(msg_t);
         qDebug() << "===offset=====" <<offset;

         //得到当前位置距离文件头的距离
         off_t   curFromHead = lseek(fd, 0 - offset,SEEK_END);

         if(curFromHead < 0)    //如果不加上这一判断的话,就会出现文件中内容重复读取                                              的情况
         {
             break;
         }

         //qDebug() <<"====curFromHead====" <<curFromHead;
         msg_t  readBuf;
         readBytes = read(fd, &readBuf, sizeof(msg_t));
         qDebug() <<"===readBytes===" << readBytes;
         if(readBytes <= 0)         //当读取的内容的大小小于或等于0时,文件读取完毕
         {
             qDebug() << "文件读取完成";
             break;
         }
          QString timeString = QString(readBuf.mesInfo);
          qDebug() << timeString;
         // ui->listWidget->addItem(timeString);
          readBytes = 0;
     }
    ::close(fd);
     qDebug()<<"222222";
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值