Problems reading a C file

Asked

Viewed 146 times

1

I am trying to open and read a C file and then print the result but when printing it does not come out complete in case it has a 32 x 32 text image.

Besides I would also like to know how I will read a variable file equal to it (for example I was thinking of storing the data in a vector and then scan with fgets).

void abriArquivo(){
    char Linha[1024];
    char *result;
    FILE *file;
    file = fopen("GIMP.pgm","rt");
    if(file == NULL){
          printf("Problemas na abertura do arquivo \n");
       }
       int i = 1;
       while(!feof(file)){
          result = fgets(Linha,1024,file);
          if(result)
             printf("Linha %d : %s "Linha);
    i++
    }
    fclose(file)
}

int main(int argc, char **argv){
    int contParam;
    char arquivo[100] = "";

    for(contParam = 0; contParam < argc; contParam++){
      if(!strcmp(argv[contParam],"-r")){
         //abrirArquivo();
         contParam += 1;
      }

      else if(!strcmp(argv[contParam],"-arquivo")){
         abrirArquivo();
         contParam += 1;
      }

     /* else if(!strcmp(argv[contParam],"-d")){
         strncpy(,argv[contParam + 1],20);
        */ contParam += 1;
      }
    }
}

Image in txt (same image as pgm)

Image in pgm

1 answer

1


Good afternoon, try to follow this logic:

stdio.h #Dar include em ambas
conio.h

void main()
{
  FILE *arq;
  char Linha[100];
  char *result;
  int i;
  clrscr();

  arq = fopen("GIMP.pgm", "rt");
  if (arq == NULL)  
  {
     printf("Problemas na abertura do arquivo\n");
     return;
  }
  i = 1;
  while (!feof(arq))
  {
      result = fgets(Linha, 100, arq); 
      if (result)  // Se foi possível ler
      printf(arq);
      i++;
  }
  fclose(arq);
}
  • Good afternoon get changing the value from 1024 to 100, thanks, but this printf(Arq) can not implement

Browser other questions tagged

You are not signed in. Login or sign up in order to post.