简体   繁体   English

逐行写入文本文件 c

[英]Write into text file line by line c

I've written a user space program to read from a kernel device line by line, somehow, the data is always overriden with each read.我编写了一个用户空间程序来逐行读取 kernel 设备,不知何故,每次读取总是会覆盖数据。 Can you please tell me how to fix my code?你能告诉我如何修复我的代码吗?

Here is the code:这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
 

#define BUFFER_LENGTH 256

int main()

{

  int  ret,fd;
  char buffer[BUFFER_LENGTH];
  FILE * fPtr;
  unsigned int i=0;

  if((fd = open("/dev/show_log_device_dev", O_RDWR)) == -1){
    perror("Failed to open the file");
  }
  //printf("/dev/show_log_device_dev opened.\n");
  //fflush(stdout);

  fPtr = fopen("log.txt", "w+");

  int bytesRead = 0;

  while (bytesRead < sizeof(buffer)) {
    int ret = read(fd, buffer + bytesRead, sizeof(buffer) - bytesRead);
    if (ret == 0)
        break;
    if (ret == -1) {  
      perror("Failed to read the message from the device.");
      return errno;
      /* Handle error */
    }
    bytesRead += ret; 
    printf("read from /dev/show_log_device_dev. %s\n",buffer);

  }
  if(lseek(fPtr,0,SEEK_SET)!=0) {
    fprintf(fPtr,"%s",buffer);
  }

  fclose(fPtr);
}

I would like the output file "log.txt" to contain all the lines written to buffer with each read line after line sith skip-line between lines.我希望 output 文件“log.txt”包含写入缓冲区的所有行,每个读取行在行之间跳过行。 Please show me the proper way to do it inctead of the fprintf attempt I've written above.请告诉我正确的方法,而不是我上面写的 fprintf 尝试。

Thank you.谢谢你。

char buffer[BUFFER_LENGTH];
while()
{...read(fd, buffer + bytesRead, sizeof(buffer) - bytesRead);}

This line is a major problem.这条线是个大问题。 Basically you are trying store the entire file in to buffer .基本上,您正在尝试将整个文件存储到buffer中。 But buffer has a small size.但是buffer的大小很小。 Instead you have to read small chunks and write back within the loop.相反,您必须读取小块并在循环内写回。

fPtr = fopen("log.txt", "w+");

This will open the log file in read/write mode, but you only need to write to it.这将以读/写模式打开日志文件,但您只需要写入它。 Since this is a log file, consider using "a" option to append to the log file.由于这是一个日志文件,请考虑对日志文件使用 append 的"a"选项。

fprintf(fout, "%s", buffer);

When you read the buffer it may not be null terminated.当您读取缓冲区时,它可能不会被 null 终止。 Try fwrite instead, or make sure buffer is null terminated before using fprintf请尝试使用fwrite ,或确保buffer在使用fprintf之前已终止 null

Usage of lseek is wrong and not necessary as noted in comments.如评论中所述,使用lseek是错误的,并且没有必要。

#define BUFFER_LENGTH 256
int main()
{
    int fd = open("/dev/show_log_device_dev", O_RDONLY);
    if(fd == -1)
    { perror("open failed"); return 0; }

    FILE* fout = fopen("log.txt", "a"); //or "w" to create new file
    if(fout == NULL)
    { close(fd); perror("fopen failed, log.txt is busy!"); return 0; }

    while (1) 
    {
        char buffer[BUFFER_LENGTH];
        int ret = read(fd, buffer, sizeof(buffer));
        if (ret == 0)
            break;
        if (ret == -1) 
        {
            perror("Failed to read the message from the device.");
            return errno;
        }
        fwrite(buffer, 1, ret, fout);
        //fprintf(fout, "%s", buffer);
    }

    fclose(fout);
    close(fd);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM