Save first word a file in c

4

I have a problem with this part of my code, I'm trying to read the lines of a file and cut only the first word of each line and then save it in an array.

The question is that I read the lines well and with strtok cut the first word and send it to print to verify and all well up there, but when I save the result of strtok in the final arrangement it only stores the first word of the last line read.

Example:

  

two roads diverged in a yellow wood

     

and sorry i could not travel both

     

and be one traveler long i stood

     

and looked down one as far as i could

     

to where it bent in the undergrowth

and as a result I expect a vector like this: "two, and, and, and, to"

but I get this: "to, to, to, to, to"

Code:

dictionary *load_word(int autor, dictionary *D_first)
  {
   FILE *date;
   char line[LONG_MAX_LINE];
   char exeption[4] = " \n\t";
   char *word;
   int j=0;
   if (autor == 1)
   {
      if ((date = fopen("test.txt", "r")) == NULL)
      {
         perror("robert_frost.txt");

      }
      while (fgets(line, LONG_MAX_LINE, date ) != "32")
      {   
         word = strtok(line, exeption); /*first word*/
         add_dictionary_first(D_first, j, word);
         j++;
      }

    fclose(date);
   }
   return D_first;  
 }
    
asked by Juan J. Mart 16.01.2018 в 20:46
source

1 answer

1

I do not know what your add_dictionary_first function does, nor do I know if the result of fgets becomes the same as the constant address "32" . If what you tried was to save the first word of each line, then you should know that strtok returns the same pointer whenever it is called, so if you read the first line and get the first word, everything will be fine, but then you read another line and call strtok , the content of the "previous" pointer remains with the content of the first word of the last line that departed with strtok .

To all this, one of the solutions is to create a copy of the chain returned by strtok , in the following lines:

while (fgets(line, LONG_MAX_LINE, date ) != "32") {   
  word = strtok(line, exeption); /*first word*/
  add_dictionary_first(D_first, j, word);
  j++;
}

With the following 1 :

/* ... while(fgets... { */

word = strtok(line, exeption);         /*first word*/
int len = strlen(word);                /* longitud de la primera palabra. */
char *tword = calloc(1, len + 1);      /* en caso de emergencia, memoria dinamica. */
memcpy(tword, word, len);              /* Clonamos el contenido del puntero de strtok */
add_dictionary_first(D_fird, j, word); /* Y por ultimo agregamos al diccionario. */
j++;

/* } // Final del while. */

To achieve this, you must make #include to the following headers to your .c file:

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

Within stdlib.h you will find calloc and others, which are functions you need when working with dynamic memory. Remember at the end of the program or when its life cycle is over, call free with each dynamic memory block reserved with malloc or calloc .

The complete code would be this:

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

/* ... Otras funciones... */

dictionary *load_word(int autor, dictionary *D_first) {
  FILE *date;
  char line[LONG_MAX_LINE];
  char exeption[4] = " \n\t";
  char *word;
  int j=0;
  if (autor == 1) {
    if ((date = fopen("test.txt", "r")) == NULL) {
      perror("robert_frost.txt");
    }
    while (fgets(line, LONG_MAX_LINE, date ) != "32") {   
      word = strtok(line, exeption); /*first word*/
      int len = strlen(word); /* longitud de la cadena retornada por strtok */
      char *tword = calloc(1, len + 1); /* +1 en caso de emergencia, memoria dinamica. */
      memcpy(tword, word, len); /* Copiamos la cadena en la memoria reservada. */
      add_dictionary_first(D_first, j, word);
      j++;
    }
    fclose(date);
  }
  return D_first;  
}

/* ... Otras, otras funciones... */

With that you should already see all the first words of each line.

1 : I've written the code in its head, if it does not work, let me know to fix it.

Greetings!

    
answered by 18.01.2018 в 13:23