Segmentation fault function fgets

Asked

Viewed 226 times

1

I’m having a problem segmentation fault in file opening txt. I know for manipulating strings, higher level liguagens, but I have no way to learn now, so I’m using C. My problem is to read a list with name of four thousand files, whose format of each is the following:

0.00053714,0.00053714,-0.00061595,0.30794,-0.00061595,0.30794,1.0001,1,0.0050735

My program takes the element that corresponds to the position defined by the variable posição_elemento=6, in that specific case equal to six. The problem that’s occurring is that I can’t perform this operation of taking the sixth element in my four thousand files, occurring segmentation fault. I think it’s some wrong parameter I’m passing to while (fgets(line, sizeof(line), arquivo2)). Follow my code below:

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

void subs(char* buffer){
   int i;
   for(i = 0; i < strlen(buffer); i++){
      if(buffer[i] == '\n')
         buffer[i] = 0; // Contrabarra zero: fim do buffer
   }
}
int main(){

   FILE *lista, *lista_nova;
   FILE *arquivo, *arquivo2;

   char buffer[50];
   char leia, line[256], *valor, *valor_escolhido;
   int cont_elemento = 1, posicao_elemento=6, i=0;
   float tempo;

   lista_nova = fopen ("lista_nova.txt", "r");

   arquivo = fopen("velocidades.txt", "wt");
   if (arquivo == NULL){
      printf ("Error opening velocidades\n");
      return 0;
      }

   while( !feof(lista_nova) ){
      fgets(buffer, 50, lista_nova);
      subs(buffer);
      arquivo2 = fopen(buffer, "r");

      if (arquivo2 == NULL){
         printf ("Error opening buffer.txt\n");
         return 0;
      }

      printf ("Arquivo = %s\n", buffer);

      while (fgets(line, sizeof(line), arquivo2))
    {
        //Pega o primeiro elemento separado por uma virgula.
        valor = strtok(line,",");

        //Obtem os outros elementos até o fim da linha.
        while (valor != NULL)
        {
            //printf("%s\n",valor);
            valor = strtok(NULL, ",");

            cont_elemento++;

            if (cont_elemento == posicao_elemento)
                valor_escolhido = valor;
        }
    }
    tempo=0.1*i;
    fprintf(arquivo,"%f\t%s\n",tempo,valor_escolhido);
    i++;
    cont_elemento=1;
   }

   fclose(arquivo);
   fclose(arquivo2);
   fclose (lista_nova);

return(0);
}
  • To get the string "0.00053714,0.00053714,-0.00061595,0.30794,-0.00061595,0.30794,1.0001,1,0.0050735" an array of at least 82 bytes. With 50 bytes fgets() reads the string twice: the first with 49 characters, the second with the rest including the '\n'.

  • @pmg but these 50 are to read the name of the file that will be opened and not the string. The person responsible for reading it has 256.

  • Ah! My mistake. One thing you must fix is the first cycle while. You should control the cycle with the result of the function fgets() (as you did for the inner cycle) instead of the function result feof().

  • Sorry @pmg but I don’t understand what you mean. Can I repeat please?

  • while (!feof(...)) { /* ... */ } this wrong: the correct way is while (fgets(...)) { /* ... */ }. Function feof() determines if the last error occurred is due to the end of the file; calling that function without previous error indicates causes extra file readings.

  • Thanks @pmg , already fixed but I still have the same problem of Segmentation fault, when it will open certain number of files. Currently it stops running the program, failing to open after file number 1016. When I run on another computer, the most I can open varies. I already got 560 and gave error, as well as 1200 in another and then error in the same way.

Show 1 more comment

1 answer

2


The "open" of arquivo2 and its "close" are not at the same level.

One runs within a cycle, another runs outside the cycle.

while (/* ... */) {
    /* ... */
    arquivo2 = fopen(/* ... */);
    /* ... */
}
/* ... */
fclose(arquivo2);

Tip: Uses a coherent indentation style.

  • I really had not attempted it myself. A ridiculous mistake that I did not realize. Thank you very much @pmg for correcting these two mistakes.

Browser other questions tagged

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