为什么'head'命令正确读取以''开头的字符串,而fgets却错误?

问题描述 投票:0回答:1

这是我想读出的文件,里面有一些意想不到的“ ”

   1 
   2 Mon Aug 28 14:29:29 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-info 
   3 Mon Aug 28 14:29:33 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-unbound 
   4 Mon Aug 28 14:29:41 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
   5 Mon Aug 28 14:29:43 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
   6 Mon Aug 28 14:29:44 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
   7 Mon Aug 28 14:29:47 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
   8 Mon Aug 28 14:29:51 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
   9 Mon Aug 28 14:29:53 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
  10 Mon Aug 28 14:31:18 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
  11 Mon Aug 28 14:33:50 2023 admin[::ffff:192.168.7.116]:59927: exi
  12 Mon Aug 28 14:33:51 2023 admin[::ffff:192.168.7.116]:59927: exi
  13 Mon Aug 28 14:33:54 2023 admin[::ffff:192.168.7.116]:59927: save config 
           14^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@Mon Aug 28 14:37:00 2023 admin[::ffff:192.168.7.116]:60207: show slot 
  15 Mon Aug 28 14:37:01 2023 admin[::ffff:192.168.7.116]:60207: show slot
  16 Mon Aug 28 14:37:03 2023 admin[::ffff:192.168.7.116]:60207: show slot
  17 Mon Aug 28 14:37:07 2023 admin[::ffff:192.168.7.116]:60207: show slot
  18 Mon Aug 28 14:37:21 2023 admin[::ffff:192.168.7.116]:60207: show slot
  19 Mon Aug 28 14:37:22 2023 admin[::ffff:192.168.7.116]:60207: show slot
  20 Mon Aug 28 14:37:27 2023 admin[::ffff:192.168.7.116]:60207: show slot

这是我的代码,有两种方法将其读取到 shell,一种使用 fgets(),另一种使用 linux 'head' cmd;

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 const int n = 20;
  6 
  7 #if 0
  8 int main(void)
  9 {
 10     FILE *fp = NULL;
 11     char line[1024];
 12     int i = 0;
 13 
 14     fp = fopen("/home/zyh/test/cli_log", "r");
 15     if (fp == NULL)
 16     {
 17         perror("failed to open cli_log\r\n");
 18         return -1;
 19     }
 20 
 21     while(fgets(line, sizeof(line), fp) != NULL)
 22     {
 23         printf("%s", line);
 24         i++;
 25         if (i >= n)
 26             break;
 27     }
 28     fclose(fp);
 29 
 30     return 0;
 31 }
 32 #endif
 33 
 34 int main(void)
 35 {
 36     char cmd[50] = "head -n 20 /home/zyh/test/cli_log";
 37 
 38     system(cmd);
 39 
 40     return 0;
 41 }

这是两种方法的结果;

一个带有 fgets 的


Mon Aug 28 14:29:29 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-info 
Mon Aug 28 14:29:33 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-unbound 
Mon Aug 28 14:29:41 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:43 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:44 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:47 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:51 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:53 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:31:18 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:33:50 2023 admin[::ffff:192.168.7.116]:59927: exi
Mon Aug 28 14:33:51 2023 admin[::ffff:192.168.7.116]:59927: exi
Mon Aug 28 14:33:54 2023 admin[::ffff:192.168.7.116]:59927: save config 
Mon Aug 28 14:37:01 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:03 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:07 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:21 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:22 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:27 2023 admin[::ffff:192.168.7.116]:60207: show slot 

一个带有头cmd的


Mon Aug 28 14:29:29 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-info 
Mon Aug 28 14:29:33 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 1 ont-unbound 
Mon Aug 28 14:29:41 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:43 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:44 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:47 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:51 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:29:53 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:31:18 2023 admin[::ffff:192.168.7.116]:59927: brief-show slot 2 ont-info 
Mon Aug 28 14:33:50 2023 admin[::ffff:192.168.7.116]:59927: exi
Mon Aug 28 14:33:51 2023 admin[::ffff:192.168.7.116]:59927: exi
Mon Aug 28 14:33:54 2023 admin[::ffff:192.168.7.116]:59927: save config 
Mon Aug 28 14:37:00 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:01 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:03 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:07 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:21 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:22 2023 admin[::ffff:192.168.7.116]:60207: show slot 
Mon Aug 28 14:37:27 2023 admin[::ffff:192.168.7.116]:60207: show slot 

你可以看到用 head cmd 打印出了错误的时间行“14:37:00”,而用 fgets 则没有; 所以我对 head cmd 如何打印出以 ' ' 开头的字符串感到困惑?

head cmd是否删除了所有'',我尝试将head cmd的输出推送到文件中,

head -n 20 cli_log > head_log
, 但我打开head_log,发现它和cli_log是一样的。

c linux shell command head
1个回答
0
投票

fgets
是一个
string
文件函数,并且
\0
在 C 字符串中具有非常特殊的含义。这就是为什么它不能像你想象的那样工作的原因。

如果文件格式不正确,请勿对函数进行字符串处理。

您需要将此文件视为二进制文件并相应地读取它

© www.soinside.com 2019 - 2024. All rights reserved.