C 如何从一个文件中读取多个问题?

C 如何从一个文件中读取多个问题?,c,C,我必须阅读一个txt文件,其中há是描述问题的第一行。它可以是a或b,然后我有一些int和as矩阵。我的程序对这两个都有效,但是在第一个问题之后,有些文件可能会有其他格式相同的文件。我不知道怎么做。请帮忙。 txt的示例: 5 5 A(B) 1 0 3 (start point)´ 1 5 6 5 9 5 8 6 3 1 8 6 9 5 3 5 6 9 3 0 2 3 9 3 8 这种格式还有一个或更多的问题 while(!feof(fp)){ fscanf(fp, "%d %d %c

我必须阅读一个txt文件,其中há是描述问题的第一行。它可以是a或b,然后我有一些int和as矩阵。我的程序对这两个都有效,但是在第一个问题之后,有些文件可能会有其他格式相同的文件。我不知道怎么做。请帮忙。
txt的示例:

5 5 A(B) 1 
0 3 (start point)´
1 5 6 5 9
5 8 6 3 1
8 6 9 5 3 
5 6 9 3 0
2 3 9 3 8
这种格式还有一个或更多的问题

while(!feof(fp)){
  fscanf(fp, "%d %d %c %d", &L, &C, &variante, &pontos);


  mapa=(int **)malloc(L*sizeof(int*));
  for(i=0; i<L; i++){
    mapa[i]=(int*)malloc(C*sizeof(int));
  }
  for(i=0; i<L; i++){
    for(j=0; j<C; j++){
      mapa[i][j]=0;
    }
  }

  if(variante == 'A') {
    fscanf(fp, "%d %d", &Linit, &cinit);
    for(i=0; i<L; i++){
      for(j=0; j<C; j++){
        fscanf(fp, "%d", &mapa[i][j]);
        printf("%d-", mapa[i][j]);
      }

      printf("\n");
    }
    possivel=varianteA(mapa, L, C, Linit, cinit, &custo);
    printf("%d\n",custo);
  }
  if(variante== 'B'){
    line=(int*)malloc(pontos*sizeof(int));
    col=(int*)malloc(pontos*sizeof(int));
    for(k=0; k<pontos; k++){
      line[k]=0;
      col[k]=0;
    }
    for(k=0; k<pontos; k++){
      fscanf(fp, "%d %d", &line[k], &col[k]);
    }
    for(i=0; i<L; i++){
      for(j=0; j<C; j++){
        fscanf(fp, "%d", &mapa[i][j]);
        printf("%d-", mapa[i][j]);
      }
      printf("\n");
    }
      possivel=varianteB(mapa, L, C, &custo, pontos, line, col);
      printf("%d %d\n", possivel, custo);
      free(line);
      free(col);
  }


  for(i=0; i<L; i++){
    int *linha;
    linha=mapa[i];
    free(linha);
  }
  free(mapa);
}
//  free(variante);
  fclose(fp);
while(!feof(fp)){
fscanf(fp、%d%d%c%d、&L、&c、&Variant、&pontos);
mapa=(int**)malloc(L*sizeof(int*);

对于(i=0;i在处理此类问题(或任何编程任务)时,编写一些伪代码通常是有益的,只需简单地说明程序需要做什么

解决问题

  • 打开文件
  • 而文件有剩余的内容
    • 读一行文字
      • 从该行中提取宽度
      • 从该行中提取高度
      • 从该行中提取问题类型“A”或“B”
      • 如果问题类型为“A”
      • 从行中提取最后一个数字
      • 如果问题类型为“B”
      • 从行中提取剩余的2+个数字
    • 读一行文字
      • 从该行中提取起点-x
      • 从该行中提取起点-y
    • 从文件中读取高度行文本
      • 对于每一行,提取数组\u数据的宽度数字
    • 读空行
(编辑:看起来我已经将缩进级别设置到了最大值,但你明白了)


因此,现在问题被分解为更小的、希望更易于管理的问题。

以下是一些示例sudo代码,介绍如何构建程序。您将不得不大量修改内容,但您说您只需要阅读第二个问题

#define MAX_SIZE (100) //or whatever it is
#define UNIQUE_IDENTIFIER "=" //or whatever it is

FILE * fp = NULL;
int problem_counter = 0;
char * line = malloc(sizeof(char)*MAX_SIZE);

if(!(fp = fopen("problem.txt","r"))){
  do_error_handling();
}

do{
printf("This is the %dth problem", ++problem_counter); //dont need to printf this but you can start counting here.
if(fgets(line,MAX_SIZE,fp) == NULL){
  break;
}
if(strstr(line,UNIQUE_IDENTIFIER){
  //start of problem.  maybe read in all the lines here and build up the problem you have to solve
  read_problem(line); //does special stuff based of the first line read
  solve_problem(); //solve the problem based on the stuff you extracted from reading the problem
}
while(!feof(fp));

请编辑您的问题,以包括一些示例输入文本编辑谢谢,问题之间有一条白线。对于类型
a
过程,第一行是:
5 a 1
?是的,5 5是矩阵大小,a或b是问题类型,最后一个数字是a的1,或是b的2plus,在第一行之后是n有一点,当文件还有剩余时,我怎么做content@JorgeProença-在处理数据时只读取所需的数据。一旦读取了数组数据的高度行,程序就会知道下一行应该为空,然后出现另一个问题。
feof()函数可以用来测试程序是否已读完了文件的末尾。因此,尝试读取空白行,如果(在读取之后)<代码> FEFF()/代码>返回true,则没有更多的问题,否则,开始阅读下一个问题-True。实际上,如果您读过空白行,则指示文件中存在新的问题。(或文件已损坏)。类似于我现在编辑的内容?是否要立即读取整行以确保其具有唯一标识符,或假设第一行开始出现问题,在读取所有行后,返回到下一个问题的开始。如果程序正常运行,请随意使用它。