使用逗号分隔的十六进制值将文件读取到C

时间:2018-09-17 06:46:46

标签: c

我如何读取带有逗号分隔的十六进制值的文本文件,例如0x58,0xA9,0x00并将其值作为数组的元素,例如LR0 我需要用从文件中读取来替换此硬编码:

const unsigned char LR0[] = {0x58,0xA9,0x00}

这是我到目前为止写的。 printf("%c", ch);向我显示了我的需求,但是当我取消注释strcat(LR0, ch);时,它在运行时失败,并显示segment fault。我不知道是否应该使用strcat或其他任何内容来附加此LR0数组的元素。

#include <stdio.h>                                                                                                                                                                                                   
#include <stdlib.h>                                                                                                                                                                                                  
#include <string.h>                                                                                                                                                                                                  

int main() {                                                                                                                                                                                                         
   int ch;                                                                                                                                                                                                           
   FILE *fp;                                                                                                                                                                                                         
   //const unsigned char LR0 [1024] = "";                                                                                                                                                                            
   const unsigned char LR0 [] = {};                                                                                                                                                                                  
   fp = fopen("test.txt", "r");                                                                                                                                                                                      
   if (fp == NULL)                                                                                                                                                                                                   
   {                                                                                                                                                                                                                 
      perror("Error while opening the file.\n");                                                                                                                                                                     
      exit(EXIT_FAILURE);                                                                                                                                                                                            
   }                                                                                                                                                                                                                 
   while((ch = fgetc(fp)) != EOF){                                                                                                                                                                                   
      printf("%c", ch);                                                                                                                                                                                              
      //strcat(LR0, (char) ch);                                                                                                                                                                                             
   }                                                                                                                                                                                                                 
   fclose(fp);                                                                                                                                                                                                       
   return 0;                                                                                                                                                                                                         
}

很抱歉,这样的基本问题无法通过谷歌搜索来解决。我不是C开发人员,并且我在Linux上使用gcc。我的文本文件不包含行,因此我无法使用this solution

2 个答案:

答案 0 :(得分:2)

您的代码中有两个问题。

  1. LR0被声明为const,其大小未指定,仅是指针,对其进行写入可能会导致UB。
  2. strcat需要它的参数为char *类型,但是第二个参数为char类型(int ch;)。

    strcat(LR0, (char) ch)

您可以将fscanf,用作分隔符,如下所示,以仅读取丢弃hex的{​​{1}}值。

,

答案 1 :(得分:1)

const unsigned char LR0 [] = {};表示长度为零的数组-某些编译器尚不支持标准C语言。

strcat(LR0, (char) ch);尝试1)写入const数组LR0和2)写入数组之外-长度仅为0。两者都是未定义行为(UB)。

  

我不知道是否应该使用strcat

使用str...()函数不能很好地处理可能包含许多"0x00, 0x00, ..."的输入。


  

我如何读取带有逗号分隔的十六进制值的文本文件,例如0x58,0xA9,0x00并将其值作为数组(?)的元素

读取文件以确定其长度和内容。我建议每个人都通过。

以下内容未经测试,但希望足以启动OP。它几乎没有错误检测。

// Parse a text file like  "0x58,0xA9,0x00"
// Return byte count.  Return 0 on error.
size_t read_comma_hex(FILE *f, unsigned char *dest, size_t num) {
  rewind(f);
  size_t i;
  for (i = 0; i<num; i++) {
    if (i) {
      int ch = fgetc(f);

      // Add to tolerate white space before the ',' or after the the last number
      while (isspace(ch)) {
        ch = fgetc(f);
      }

      if (ch == EOF) break;    // no more data
      if (ch != ',') return 0; // Fail, as ',' expected
    }
    unsigned char hex;
    if (fscanf(f, "0x%hhx", &hex) != 1) return 0;   
    if (dest) dest[i] = hex;
  }
  return i;
}

void read_comma_hex_file(FILE *f) {
  size_t n =  read_comma_hex(f, NULL, SIZE_MAX);
  if (n == 0) return; // no data or failure

  // OP wants an array - research variable length array
  const unsigned char LR0[n];
  // Alternative: allocate memory instead of using an _array_. (not shown)

  // Read data into the array  
  read_comma_hex(f, LR0, n);

  // use LR0 and n some how
  for (size_t i = 0; i<n; i++) {
    printf("0x%02hhX%s", LR0[i], i > 0 ? "," : "");
  } 
}
相关问题