Thread: HELP Homework correction!!!

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    16

    HELP Homework correction!!!

    Hello everybody I need Your help:
    I've written the code below as homework but I can't get it to work!

    the aim of the program is to extract email addresses from a txt file (our professor wants to make us spammer ) it receives in input a first file which contains the filename of other files in which is the text to analyze.
    the program has to scan the text in search for an email address (is considered email any string from 'space to space' which contains the symbol @ once!
    The program has to produce in output a file with the list of email extracted and has to print the number of email found (total) and the number of email found (removing the doubles).


    I realized the program the following way:
    -read file 1
    -read address of file(s)2 from file1 (till the EOF)
    ---read emails and save to dynamic list
    ---order the list alphabetically and remove the doubles
    -save the clean and ordered list to the output file
    -print the number f email total
    -print number of email "clean"
    -end

    the program runs ok untill he enter the function "ordinalista" which orders the list and removes the doubles! I suspect I've made some mistake with pointers or while building the list.

    any way this is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_STR 25
    
    typedef enum {FALSE, TRUE} boolean;
    typedef struct dati_s
    {
        char email[MAX_STR];
        struct dati_s *next;
    } dati;
    dati *p_testa, *p_corr;
    int n_netto, n_tot=0;
    
    /*PROTOTYPES*/
    boolean leggifile(char nomefile[MAX_STR]);
    void ordina_lista(dati **p_dati);
    boolean salva_su_file(dati **puntdati, char f_out[MAX_STR]);
    
    /*MAIN*/
    int main()
    {
        FILE *file1; 
        char nf_in[MAX_STR], nf_out[MAX_STR];
    
        printf("Inserire il nome del file da creare in output:\n");
        scanf("%s", nf_out);
    
        if((file1=fopen("files.txt", "r"))==NULL)
            printf("ERRORE APERTURA FILE!!!\n");
        else{
            int stato;
    
            p_testa=NULL;
            for(stato=fscanf(file1,"%s",nf_in);stato!=EOF;stato=fscanf(file1,"%s",nf_in))
            {
                if(leggifile(nf_in)==FALSE)
                    printf("ERRORE!!!\n");
            }
            printf("numero totale email estratte: %d\n", n_tot);
            ordina_lista(&p_testa);
            printf("numero email senza doppioni: %d\n", n_netto);
            if(salva_su_file(&p_testa, nf_out)==TRUE) 
                printf("salvataggio effettuato correttamente!");
            else
                printf("ERRORE SALVATAGGIO FILE!!!");
        }
        fclose(file1); 
        return 0;
    }/*END MAIN*/
    
    /*FUNCTIONS*/
    boolean leggifile(char nomefile[MAX_STR])
    {
        int i=0,k;
        boolean opero, in_parola=TRUE, chiocciolina=FALSE;
        char t_email[MAX_STR], carat;
        FILE *file2; 
    
        if((file2=fopen(nomefile,"r"))==NULL)
        {
            printf("ERRORE APERTURA FILE!!!");
            opero=FALSE;
        }else{
            opero=TRUE;
            do{
                carat=getc(file2); /*confuso nomefile con file2*/
                /*int c;
                c=isalpha(carat);*/
                if(((isalpha(carat)!=0)||(carat=='@')||(carat=='.'))&&(i<MAX_STR)) 
                {
                    in_parola=TRUE;
                    t_email[i]=carat;
                    i++;
                    if((chiocciolina==FALSE)&&(carat=='@')) 
                        chiocciolina=TRUE;
                }else{
                    in_parola=FALSE;
                    t_email[i+1]='\0'; 
                    i=0; 
                    if(chiocciolina==TRUE)
                    {
                        p_corr=(dati*)malloc(sizeof(dati));
                        for(k=0;k<i;k++) 
                        {
                            (*p_corr).email[k]=tolower(t_email[k]);
                            t_email[k]='\0'; 
                        } /*end for*/
                        n_tot++;
                        (*p_corr).next=p_testa;
                        p_testa=p_corr;
                        chiocciolina=FALSE; 
                    } /*end if*/
    
                } /*end if-else*/
            }while(carat!=EOF);
        } /*end if-else*/
        fclose(file2); 
        return(opero);
    } /*end leggifile*/
    
    
    void ordina_lista(dati **p_dati)
    {
        int h, j=0;
        dati *p_corr1, *p_corr2;
    
    /*ordino lista*/
        char temp_email[MAX_STR];
    
        p_corr1=*p_dati;
        p_corr2=(*p_corr1).next;
    
        
    
        while(p_corr1!=NULL)
        {
            while(p_corr2!=NULL)
            {
                if(strcmp((*p_corr1).email,(*p_corr2).email)>0)
                {
                    strcpy(temp_email,(*p_corr1).email);
                    strcpy((*p_corr1).email,(*p_corr2).email);
                    strcpy((*p_corr2).email,temp_email);
                }
                p_corr2=(*p_corr2).next;
            }
            p_corr1=(*p_corr1).next;
        }
    
        /*elimino doppioni*/
        p_corr1=*p_dati;
        p_corr2=(*p_corr1).next;
        while(p_corr2!=NULL)
        {
            h=strcmp((*p_corr1).email,(*p_corr2).email);
            if(h==0)
            {
                j++;
                n_netto=n_tot-j;
                (*p_corr1).next=(*p_corr2).next;
                free(p_corr2);
                p_corr2=(*p_corr1).next;
            }else{
                p_corr1=(*p_corr1).next;
                p_corr2=(*p_corr2).next;
            }
        } /*end while*/
    
    
    }/*end ordina_lista*/
    
    boolean salva_su_file(dati **puntdati, char f_out[MAX_STR])
    {
        boolean ok;
        dati *puntcorr;
        FILE *file_out;
    
        if((file_out=fopen(f_out, "w+"))==NULL)
        {
            ok=FALSE;
        }else{
            while(puntcorr!=NULL)
            {
                fprintf(file_out, "%s", (*puntcorr).email);
                puntcorr=(*puntcorr).next;
            }
            ok=TRUE;
        }
        fclose(file_out);
        return(ok);
    }/*end salv_su_file*/
    I hope it's clear enough, I apologize for my bad English! also the code is "written" in Italian...

    Thank You in advance for the time dedicated to me.

    Andryjohn
    Last edited by andryjohn; 09-05-2009 at 07:41 AM. Reason: code update

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Correction !!Urgent!!
    By andryjohn in forum C Programming
    Replies: 12
    Last Post: 07-10-2009, 12:29 PM
  2. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM

Tags for this Thread