Thread: Help with a program! (file, pointers,strings)

  1. #1
    Registered User wildarms's Avatar
    Join Date
    May 2010

    Angry Help with a program! (file, pointers,strings)

    Hi everyone, these last days ive been trying to make an assigment my teacher left, i think i have the basic idea of everything, but i just cant get it working correctly at the end.

    Im starting to lose my patience since my teacher wont even answer her e-mails (she never does on weekends...) and the test is this tuesday.

    The assigment is in spanish (im a spanish native speaker) but i will translate all i can, i also used spanish words when putting names in the program, but it shouldnt make it too hard to understand, ill put notes explaining everything anyway.

    I know it may be kinda big, but i would really appreciate the persons that help me on this one.

    This is the assigment:

    Starting from a text archives (called "palabras.txt") ((this one was gave to us by the teacher)) , make a list and determine the number of times a word is repeated on the file, and at the end, make a new .txt file called "salida.txt" that contains the words and the number of times it was repeated.

    We just put all the files, the program, the .exe file is made when you compile the program and the palabras.txt in the same folder so we dont have to bother with the file's direction.

    For example:
    If the palabras.txt file has: L = { blue,red,yellow,red,yellow,red } the salida.txt would be :

    blue 1
    red 3
    yellow 2
    And well, this is the program ive been doing, but it just doesnt goes out right way and i cant find the problem...

    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct campos {
        char valor[10]; /*will contain the words*/
        int repetido; /*will contain the times the word is repeated*/
        struct campos *prox; /* "prox" will be the one that represents the value/space/whatever after the
        memory the pointer is pointing*/
    typedef struct campos Nodo; /*change the name to "Nodo"*/
    Nodo  *primero;
    void Crear_Lista() /*making a list, using pointers to put the words*/
     {primero = NULL;}
     /*primero would always be pointing at the beginning of the list*/
    char EsListaVacia() /*made to see if the list is empty*/
      { if (primero == NULL)
          return 1;
          return 0; 
    void Insertar(char val[])   /*this one is supposed to insert the words into a list*/
       Nodo *anterior,*aux;
       aux = (Nodo*)malloc(sizeof(Nodo)); 
       strcpy(aux->valor,val); /*storing the word on the memory this pointer is uh.. pointing*/
       aux->prox = NULL;
       if(EsListaVacia()==1)  /*if the list is empty, i just make the aux the first one on the list*/
          {primero = aux;}
               anterior = primero;
               while(anterior->prox != NULL)
                    {anterior = anterior->prox;}
              aux->prox = anterior->prox;
              anterior->prox = aux;
             } /*its easier to see if you draw it on a paper, when anterior has a NULL next to it (and aux had
             Null after it from the beginning, the next from aux becomes next of anterior (so it becomes NULL
             and the next of anterior becomes aux, so now it comes anterior->aux->NULL, so everytime i call 
             "insertar" the words will be added in order */
    void InsTexto() /*ok, here is where i started doing the new stuff, i made this one to insert the words into the
                       pointer's list*/
    {FILE *pal;
    char palabra[10];
    if (!(pal=fopen("palabras.txt","r")))  /*if there is no file like this it will give this printf*/
    {printf("Error al abrir uno de los dos archivos, porfavor intente de nuevo \n");} /*this just says that
                                                                        there was an error opening the file*/
    {while (!feof(pal)){   /*so, while the file pal (wich is palabras.txt) doesnt hits the end symbol*/
          fgets(palabra,10,pal); /*it gets the word from the list, and put it into palabra*/
          Insertar(palabra);}  /*then, it is inserted into the list, note that here the while is closed*/
          fclose(pal); /*close the file*/
             }printf ("insertado\n");system ("pause"); /*used it to know when what is happening, when i see this
                                                    i know the program could do the "InsTexto()"*/
    void comparacion()   /*this one is supposed to compare the words between them, to know how many times they
                          apprear repeated*/
         Nodo *aux1,*aux2,*numerador;
         aux2=primero;            /*i put all these 3 pointers to point to the first space of the list*/
         int m1=0; /*this will keep the track on how many times it is repeated*/
        while (aux1!=NULL)/*while aux1 doesnt get to the end of the list*/
         while(aux2!=NULL)/*while aux2 doesnt get to the end of the list*/
           if (strcmp(aux1->valor,aux2->valor)== 0) /*if the strcmp gives the 0, it means the words are the
                                                        same so...*/
                {m1++;}/*i put one more to the m1, to know it already has been repeated once (here, i close the if*/
           numerador->repetido=m1; /*i insert the value of m1 into the slot i made on the struct for times repeated*/
           numerador=numerador->prox; /*Now numerador goes into the next memory to insert the next value into it*/
           m1=0;/*i reset m1 back to 0 again*/
           aux2=primero; /*i put aux2 pointing to the first one again*/
         aux1=aux1->prox; /*i advance aux 1 and the while loop should start again*/
         }  printf ("comparado\n"); system ("pause");    /*same as before, i put this to know this part has been
                                                        succesfully done*/
    void creacion()   /* and this is the last one, it will create the "salida.txt" with all the things i was asked for*/
    FILE *fin;  
    Nodo *aux1;
    aux1=primero;  /*i make aux1 to point at the beginning of the list*/
    fin=fopen("salida.txt","w");   /*i open the new salida.txt, and make it "fin"*/
     while (aux1!=NULL)  /*while aux1 doesnt reach the NULL*/
     fprintf(fin,"%s\n",aux1->valor); /* this should put the word that was already stored into the new text file*/
     fprintf(fin,"%d\n",aux1->repetido); /*this should put the numbers i stored before into the new file*/
     aux1=aux1->prox;  /*and then i just advance the pointer*/
    fclose(fin);printf ("creado\n");system ("pause"); /*close the salida.txt, and the same print i use to know
                                                        if this part have been done*/
          printf ("Presione cualquier tecla para ejecutar programa\n"); /*translation: press any key to start the
          system ("pause");
          InsTexto();  /*run the function i made to put the words into a list*/
          comparacion(); /*compare words between them and insert the number of times repeated into the list*/
          creacion();  /*create the salida.txt*/
    Last edited by wildarms; 11-21-2010 at 12:34 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > fin=fopen("salida.txt","w"); /*i open the new salida.txt, and make it "fin"*/
    This should be outside your while loop.

    Your condensed and erratic indentation makes it hard to spot what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User wildarms's Avatar
    Join Date
    May 2010
    Quote Originally Posted by Salem View Post
    > fin=fopen("salida.txt","w"); /*i open the new salida.txt, and make it "fin"*/
    This should be outside your while loop.

    Your condensed and erratic indentation makes it hard to spot what is going on.
    Sorry!, ill be sure to fix that if i have a problem again, man! thanks a lot!, now it goes out! If i knew you i would kiss you.
    Edit: i will fix this one as well just in case somebody else want to take a look at it,... already fixed what you told me as well, there is still something weird, a number is always repeating itself after the word and the numer that says how many times the word is repeated.

    Like for example,
    yellow 4 52
    red 3 52
    blue 2 52

    But at least it goes out, that is what was killing me.
    Last edited by wildarms; 11-21-2010 at 12:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM