-1

I have to read a txt file which has the first line to describe the problem. It can be either a or b, then I have some ints and as matrix. My program works for both, but there are files that after the first problem may have another with the same format. I don't know how to do this.

sample of 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

then another problem, or more, with this format

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);

now I have this, but it does one more problem that is not in the file. and valgrind gives me an error in: possivel=varianteA(..). It says that address is 0 bytes after block of size 24 alloc'd

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

When tackling this sort of problem (or any programming task, really), it's often beneficial to write some pseudo-code, just in words about what the program needs to do.

Do Problem

  • Open the file
  • While file has remaining content
    • read a line of text
      • extract the width from that line
      • extract the height from that line
      • extract the problem-type "A" or "B" from that line
      • IF problem-type is "A"
      • extract the ??? last number from the line
      • IF problem-type is "B"
      • extract the remaining 2+ numbers from the line
    • read a line of text
      • extract the start-point-x from that line
      • extract the start-point-y from that line
    • read height lines of text from the file
      • With each line, extract width numbers for array_data
    • read a blank line

(EDIT: It looks like I maxed-out the indentation levels, but you get the idea)

So now the problem is broken up into smaller, hopefully more manageable problems.

Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • how can i do the part of while file has remaining content – Jorge Proença Nov 14 '18 at 23:34
  • @JorgeProença - Only read the data you need as you are dealing with it. Once you have read the **height** lines of array data, the program knows the next line should be blank, and then there's another problem. The `feof()` function can be used to test if the program has read past end-of-file. So try to read the blank line, and if (after that read) `feof()` returns true, there is no more problems, otherwise, start reading the next problem-text. – Kingsley Nov 14 '18 at 23:36
  • Actually, if ever you read a blank line, it indicates there is a new problem in the file. (or the file is corrupt). – Kingsley Nov 14 '18 at 23:39
0

Here is some sample sudo code on how it may be possible to structure your program. You will have to modify things heavily but you said you just needed to read the second problem.

#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));
Bwebb
  • 675
  • 4
  • 14
  • something like i eddited now? – Jorge Proença Nov 14 '18 at 23:54
  • do you want to read the whole line at once to make sure it has a unique identifier, or assume the first line starts the problem and after you read all the lines your back to the start of the next problem. If your program works feel free to use it. – Bwebb Nov 15 '18 at 00:36