seg fault in text parser

This is a discussion on seg fault in text parser within the C Programming forums, part of the General Programming Boards category; I have a seg fault in this function and i can't seem to pin it down. Appreciate any help you ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    seg fault in text parser

    I have a seg fault in this function and i can't seem to pin it down. Appreciate any help you guys could give.Thanks.
    Code:
    int AllocateFile(FILE *filename,long num_of_records){
    /*iterates through file contents and adds each character to the appropiate field*/
     
        int ndx=0;
        int field=0;/*ndx for field ndx
        next for temp counter and field for what field number we in*/
        size_t word_length=0;
        int character;
        int comma_count=0;
        char buffer[BUFSIZ];/*really holding integers should put it to char*/
        RECORD person[num_of_records];/*array of records named person to hold each line in file*/
       
    /*go through file and puts contents in the array*/
    while ( (character=fgetc(filename)) !=EOF){
             buffer[ndx++]=character;
              
              if ( (buffer[ndx] =='\n') )
                 field+=1;/*if its a newline then its a new field*/
                 
              if ( (buffer[ndx] == ',') ){
                comma_count+=1;/*update comma count so know where we are in file*/
                switch(comma_count){
                  case 1:
                  buffer[ndx-1]='\0';/*terminate and make buffer a string*/
                  strcpy(person[field].field1,buffer);
                  buffer[ndx]= buffer[ndx-word_length];/*buffer ndx goes to zero*/
                  puts(person[field].field1);
                  break;
                  case 2:
                  buffer[ndx-1]='\0';
                  break;
                  }//end of break
    } }}

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Seg fault could be in a couple places.
    First, you never check if you overwrite your buffer.
    Second, the fault could come from when you try to copy the string into person[field].field1. Does this buffer have enough space?
    Third, you need to make sure that num_of_records is correct, and there is actually that many records. If there is more than num_of_records, then your person array will overflow.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Fourth, what the hell is wrong with people? Why does no one here ever indent their code cleanly? No one wants to read that .........


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > RECORD person[num_of_records];
    This isn't even C - no variable length arrays for you.

    > if ( (buffer[ndx] =='\n') )
    I wonder, given the previous use of ++, what this reads as?

    > strcpy(person[field].field1,buffer);
    Now is field1 an array, and if it is, is it big enough?
    Is it a pointer, and if it is did you allocate it?

    > buffer[ndx]= buffer[ndx-word_length];/*buffer ndx goes to zero*/
    Hah - this does nothing because word_length is always 0
    So all you do is perform a useless assignment of a variable to itself.
    Meanwhile, ndx continues it's relentless march through memory, trashing anything when your file is > BUFSIZ in size, and your strcpy continues to copy the first field regardless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 02:17 PM
  3. Access Violation / Seg Fault
    By Korhedron in forum C++ Programming
    Replies: 31
    Last Post: 09-06-2008, 12:47 PM
  4. Found a seg fault, but can't explain why i get it
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2005, 03:58 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21