You are here:

C/Segmention Fault

Advertisement


Question
Hi, im a begginer in c, im portuguese, and my english is too bad, sorry.

My question is about my University project, that i got a segmention fault, and i dont know why...

my function is that:

#define MAXLEN 1025

typedef struct{
       char word[MAXLEN];
       int   length;
       }spamword

void makevectorspam(spamword *vspam,int maxlines,FILE *fp)//int maxlines,
   {
    int len=0,cont=0,nlines=0;
    char line[MAXLEN], ch;
           while (((len = strlen(fgets(line,13,fp)))>0) && (nlines<=maxlines)) { //
                        cont++;
                        vspam[nlines].length = len-1;
                        line[len-1] = '\0'; // delete newline
                        strcpy(vspam[nlines].word, line);
         nlines++;
         printf("%d\n",len);
         printf("%s\n",vspam[nlines-1].word);
     
                   }
printf("ola");

}

i run tha with this main

main()
{
     FILE *fp,*fp2;
     fp=fopen("in01.spam","r");
     fp2=fopen("spam.txt","r");
     int n;
     spamword *spam, *vsp;

     spam=(spamword *) malloc (sizeof(spamword));//numero linhas mult por size
     vsp=(spamword *) malloc (20*sizeof(spamword));
     makevectorspam(vsp,10,fp);
//       printf("%s ----- %d\n",vsp[2].word,vsp[2].length);
      makestring(spam, fp2);
      makestring(spam, fp2);
      n=spamcompare(vsp[0],spam[0]);
  printf("\n%s----%d\n",vsp[2].word,vsp[10].length);
  printf("\n%s----%d\n",spam[0].word,spam[0].length);
  printf("-------------%d---------\n",n);
     free(spam);
     free(vsp);
  fclose(fp);
  fclose(fp2);
}

my problem is in the file, i got 11 lines in my files, if i put the function read 9 thats alright, but if i put the 11 lines, i got the segmention fault...i try to mutch things, but nothing, i hope u can help me, my time is going...and i dont know what i can do...

The objective of my function (i dont know if i can express me) is get a array of pointers of my struct spamword, to spamword.word...exemple

spamword[0].word ---> "SPAM"
spamword[1].word ---> "WINDOWS"
etc
etc
etc
until EOF.

i wanna create a array, to compare with string that i read on email, to verificate tha it is a spam word or not.

tks 4 all

Answer
Hi,

    I have modified the make vectorspam() function.
    Can you please try this and let me know whether it works or not?

Regards,
Narendra


void makevectorspam(spamword *vspam,int maxlines,FILE *fp)//int maxlines,
{
       int len=0,cont=0,nlines=0;
       char line[MAXLEN], ch;

       while ((!feof(fp)) && (nlines<=maxlines))
       {
               if (fgets(line,13,fp) != NULL)
                       len = strlen(line);
               else len = 0;
               cont++;
               vspam[nlines].length = len-1;
               line[len-1] = '\0'; // delete newline
               strcpy(vspam[nlines].word, line);
               nlines++;
               printf("%d\n",len);
               printf("%s\n",vspam[nlines-1].word);
       }
       printf("ola");
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.