【问题标题】:How to read in a file of mixed data types into a string in C如何将混合数据类型的文件读入C中的字符串
【发布时间】:2017-04-28 07:26:53
【问题描述】:

我正在尝试读取格式如下示例的文件:

3 3 1.5 50
0 2 46.0 0
* 1 46.0 1
2 * 46.0 0
0 0 50.0 0 
* * 42.0 0
2 2 36.1
2 1 42 0
0 1 48.0 0
1 0 48 0

首先我想将文件的内容存储在一个字符串中。然后,我想扫描字符串并查看是否有任何星号 *.出于某种原因,我无法将其存储为字符串。每当我尝试打印字符串时,它都会给我空白行。有没有一种简单的方法可以从文件中读取数据并将其存储到字符串中?稍后我会将数值数据转换为数组。

代码sn-p:

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

int main(int argc, char *argv[]){ 

  FILE *id; //Input Data
  double *detect; 
  int nx, ny, i, j, k, n, count, t, frequency;
  double a;
  char val;

  n = 0;

  id = fopen(argv[1], "r");
  frequency = atoi(argv[2]);

 if(id){
   fscanf(id, "%d %d %lf %d", &nx, &ny, &a, &tn);
  }

  detect = (double *) malloc(nx*nx*4*sizeof(double)); 

  if(id){
    for(i=0; i<2; i++){
      fscanf(id,"%s", (detect+i));
    }
  }  


//~ The rest of the code is left out ~


return 0;
}

【问题讨论】:

  • * 无法使用%d 读取。最快修复:使用%c 并检查读取的值是否为*,否则将其用作数字。
  • 为什么要使用double类型来读取为字符串?
  • 你为什么要分配nx * sizeof(double)字节?
  • 使用fgets
  • 但是使用double * 读取字符串仍然很奇怪......

标签: c file input


【解决方案1】:

有几种方法可以做到这一点...您可以使用 fscanf、正则表达式、标记器、解析生成器、有限状态机...

各有优缺点... fscanf 可能会因输入无效而出现错误,正则表达式可能需要很长时间来解析整个文件,标记器是一个复杂的工具,您可能需要一段时间来学习(Flex 是你),要使用 FSM,你需要真正知道自己在做什么......

【讨论】:

    【解决方案2】:

    你可以把数据变成这样的字符串。

    它使用以下内容:

    1. fgets 从文件中读取每一行并存储在字符串缓冲区中。
    2. malloc 为字符串分配空间,char *detect 在堆上。需要时使用realloc 重新分配更多空间。
    3. strcatbuffer 附加到指针*detect
    4. free 释放malloc() 请求的内存。

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFFSIZE 100
    #define STARTSIZE 10
    
    int
    main(int argc, char *argv[]) {
        FILE *id;
        char *detect;
        char buffer[BUFFSIZE];
        size_t slen, currsize = STARTSIZE, len = 0;
        const char *separate = " ";
    
        id = fopen(argv[1], "r");
        if (!id) {
            fprintf(stderr, "%s\n", "Error reading file");
            exit(EXIT_FAILURE);
        }
    
        detect = malloc(currsize * sizeof(*detect));
        if (!detect) {
            printf("Error allocating memory\n");
            exit(EXIT_FAILURE);
        }
    
    
        *detect = '\0';
        while (fgets(buffer, BUFFSIZE, id) != NULL) {
            slen = strlen(buffer);
            len += slen-1;
            if (slen > 0) {
                if (buffer[slen-1] == '\n') {
                    buffer[slen-1] = '\0';
                } else {
                    printf("Error: Exceeded Buffer length of %d.\n", BUFFSIZE);
                    exit(EXIT_FAILURE);
                }
            }
    
            if (currsize == len) {
                currsize *= 2;
                detect = realloc(detect, currsize * sizeof(*detect));
                if (!detect) {
                    printf("Error reallocating memory\n");
                    exit(EXIT_FAILURE);
                }
            }
    
            strcat(detect, separate);
            strcat(detect, buffer);
        }
    
        printf("Your string = %s\n", detect);
    
        free(detect);
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      我要做的是创建一个所有数据类型的结构存储在一个文件中,然后创建一个该结构的数组,具体取决于文件中的行数文件,然后使用循环,我将使用fread 将数据读入这些结构,并在 显式转换为 int 的所有数据类型 ASCII 值为星号。

      【讨论】:

        【解决方案4】:

        我从上面的代码中了解到,您将 detect 指针声明为 Double。在第二个 fscanf 中,您使用 "%s" 从文件中读取数据作为字符串但是 detect 指针是 Double类型,这是导致问题的原因。将 detect 指针声明为 char

        char *detect;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-17
          • 1970-01-01
          • 2017-10-04
          • 2013-03-14
          • 1970-01-01
          • 2013-01-03
          • 1970-01-01
          相关资源
          最近更新 更多