繁体   English   中英

使用逗号分隔的十六进制值将文件读入C中的数组

[英]Read file with comma separated hex values into array in 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。 我的文本文件不包含行,因此我无法使用此解决方案

您的代码中存在两个问题。

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

    strcat(LR0, (char) ch)

您可以使用fscanf,如分隔符,如下仅读取hex值丢弃,

int main() {                                                                                                                                                            
    FILE *fp;                                                                                                                                                            
    unsigned char LR0 [1024] = {};                                                                                                                                          
    fp = fopen("test.txt", "r");                                                                                                                                         
    if (fp == NULL) {                                                                                                                                                                    
        perror("Error while opening the file.\n");                                                                                                                        
        exit(EXIT_FAILURE);                                                                                                                                               
    }

    int i = 0;                                                                                                                                                          
    while(fscanf(fp, "%c,", &LR0[i]) == 1){                                                                                                                                
        printf("%c", LR0[i++]);                                                                                                                                           
    }

    fclose(fp);                                                                                                                                                          
    return 0;                                                                                                                                                            
}

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 ? "," : "");
  } 
}

暂无
暂无

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

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