如何将混合数据类型的文件读入C中的字符串

时间:2016-12-13 09:34:01

标签: c file input

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

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

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

代码段:

#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;
}

4 个答案:

答案 0 :(得分:2)

您可以将数据放入这样的字符串中。

它使用以下内容:

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

    #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;
    }
    

答案 1 :(得分:1)

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

char *detect;

答案 2 :(得分:1)

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

答案 3 :(得分:0)

有几种方法可以这样做...你可以使用fscanf,正则表达式,tokenizer,parse generator,有限状态机......

所有都有优点和缺点... fscanf可能误导无效输入,正则表达式可能需要很长时间来解析整个文件,标记化器是一个复杂的工具,你可能需要一段时间来学习(Flex是那个人你使用FSM,你需要真正了解自己在做什么......