Jump to content

Segmantation fault of the code: compiles but wrong output

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Kubinec

Kubinec

    Newbie

  • Members
  • Pip
  • 6 posts
Hi,

I got this code done and have got some problems with output:

insert
Code:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

/*********************************/
/* structure EastDevon*/

struct EastDevon {
    char party[2];
    char lastName[31];
    char firstName[31];
    unsigned int votes[4];
    struct EastDevon *nextresult;
    };
typedef struct EastDevon RESULT;

/* function prototypes */

RESULT ProcessFile (FILE *);
void AddResult     (RESULT *, char*, char *, char *, unsigned int, unsigned int, unsigned int, unsigned int);
void PrintList (RESULT *);

/* main function */

int main() {
    FILE *fin;
    RESULT root;
    if ((fin = fopen("EastDevon.txt", "r")) == NULL)
    {
    printf ("Unable to read file\n");
    return EXIT_FAILURE;
    }
    
    root = ProcessFile(fin);
    PrintList(&root);
    
    fclose(fin);
    return EXIT_SUCCESS;
}

/*Reads the file pointed to by fin and constructs a linked list structure,
/* with root as the root result of the tree;     */

RESULT ProcessFile (FILE *fin){
    RESULT root;
    char cParty[2];
    char cLastName[31];
    char cFirstName[31];
    unsigned int C1Votes;
    unsigned int C2Votes;
    unsigned int C3Votes;
    unsigned int C4Votes;
    int count = 0;
    
    while (fscanf (fin, "%s%s%s%d%d%d%d", cParty, cLastName, cFirstName, &C1Votes, &C2Votes, &C3Votes, &C4Votes) == 4)
  {
    count++;
    if (count == 1)
    { 
        strcpy(root.party, cParty);
        strcpy(root.lastName, cLastName);
        strcpy(root.firstName, cFirstName);
        root.votes[0]= C1Votes;
        root.votes[1]=C2Votes;
        root.votes[2]=C3Votes;
        root.votes[3]=C4Votes;
        root.nextresult = NULL; }
    else
      AddResult (&root, cParty, cLastName, cFirstName, C1Votes,C2Votes,C3Votes,C4Votes);
  }
    return root;
}


/*Add a new result to the linked list whose root result is in the first
/*parameter passed to the function    */


void AddResult  (RESULT *root,
        char *cParty,
        char *cLastName,
        char *cFirstName,
         unsigned int C1Votes,
         unsigned int C2Votes,
         unsigned int C3Votes,
         unsigned int C4Votes)
{
    RESULT *result = root;
    RESULT *newresult = (RESULT *)malloc(sizeof(RESULT));
    
    if (newresult == NULL)
    { 
        printf("Unable to allocate memory\n");
        exit(EXIT_FAILURE);
    }
    
    strcpy(newresult->party, cParty);
    strcpy(newresult->lastName, cLastName);
    strcpy(newresult->firstName, cFirstName);
    newresult->votes[0] = C1Votes;
    newresult->votes[1]=C2Votes;
    newresult->votes[2]=C3Votes;
    newresult->votes[3]=C4Votes;
    
    while (result->nextresult !=NULL)
        result = result->nextresult;
    
    result->nextresult = newresult;
}

/* print all the results in the linked list */

void PrintList (RESULT *result)
{
    if (result == NULL)
    return;
    
    printf("RESULTS LIST REPORT FOR:EAST DEVON:/n %s:%s:%s:%08d %08d %08d %08d\n", result->party, result->lastName,
           result->firstName, result->votes[0], result->votes[1], result->votes[2], result->votes[3]);
    PrintList(result->nextresult);

}

The code compiles and runs, but outputing smth different from what it should and giving segmantation fault in the end:

Terminal:
RESULTS LIST REPORT FOR:EAST DEVON:/n mith:th:���I�:13520032 -1075779400 02021020 134515042
RESULTS LIST REPORT FOR:EAST DEVON:/n r:EastDevon.txt:le:1952539503 1701650533 2037542765 00000000
Segmentation fault

Here is the actual format it should be outputed in on the screen from a text file(the text file contains the same info):

ASmith Angela 214003
CJones Brian 32867
BParker-Bowles Philip 546382
GEvans Charlotte 34869837

Any idea what is wrong?

Edited by Alexander, 08 December 2010 - 01:08 PM.
(code tags)


#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The best way to solve the problem is to debug the code, and find out the point where its doing output wrong!

Munir

#3
Kubinec

Kubinec

    Newbie

  • Members
  • Pip
  • 6 posts
I did sove the problem. The problem was in data file which i was supported with. Party accepts just one charachter. In data file it was split with lastName. Thank you: This is what I ended up with:

#include <stdio.h>

#include <stddef.h>

#include <stdlib.h>

#include <string.h>



/* structure EastDevon*/


struct EastDevon {

    char party[2];

    char lastName[31];

    char firstName[31];

    unsigned int votes[4];

    struct EastDevon *nextresult;

    };

typedef struct EastDevon RESULT;


/* function prototypes */


RESULT ProcessFile (FILE *);

void AddResult     (RESULT *, char*, char *, char *, unsigned int, unsigned int, unsigned int, unsigned int);

void PrintList (RESULT *);


/* main function */


int main() {

    FILE *fin;

    RESULT root;

    if ((fin = fopen("EastDevon.txt", "r")) == NULL)

    {

    printf ("Unable to read file\n");

    return EXIT_FAILURE;

    }

    

    root = ProcessFile(fin);

    PrintList(&root);

    

    fclose(fin);

    return EXIT_SUCCESS;

}


/*Reads the file pointed to by fin and constructs a linked list structure,

/* with root as the root result of the tree;     */


RESULT ProcessFile (FILE *fin){

    RESULT root;

    char cParty[2];

    char cLastName[31];

    char cFirstName[31];

    unsigned int C1Votes;

    unsigned int C2Votes;

    unsigned int C3Votes;

    unsigned int C4Votes;

    int count = 0;

    

    while (fscanf (fin, "%s%s%s%u%u%u%u", cParty, cLastName, cFirstName, &C1Votes, &C2Votes, &C3Votes, &C4Votes) == 7)

  {

    count++;

    if (count == 1)

    { 

        strcpy(root.party, cParty);

        strcpy(root.lastName, cLastName);

        strcpy(root.firstName, cFirstName);

        root.votes[0]= C1Votes;

        root.votes[1]=C2Votes;

        root.votes[2]=C3Votes;

        root.votes[3]=C4Votes;

        root.nextresult = NULL; }

    else

      AddResult (&root, cParty, cLastName, cFirstName, C1Votes,C2Votes,C3Votes,C4Votes);

  }

    return root;

}



/*Add a new result to the linked list whose root result is in the first

/*parameter passed to the function    */



void AddResult  (RESULT *root,

        char *cParty,

        char *cLastName,

        char *cFirstName,

         unsigned int C1Votes,

         unsigned int C2Votes,

         unsigned int C3Votes,

         unsigned int C4Votes)

{

    RESULT *result = root;

    RESULT *newresult = (RESULT *)malloc(sizeof(RESULT));

    

    if (newresult == NULL)

    { 

        printf("Unable to allocate memory\n");

        exit(EXIT_FAILURE);

    }

    

    strcpy(newresult->party, cParty);

    strcpy(newresult->lastName, cLastName);

    strcpy(newresult->firstName, cFirstName);

    newresult->votes[0] = C1Votes;

    newresult->votes[1]=C2Votes;

    newresult->votes[2]=C3Votes;

    newresult->votes[3]=C4Votes;


    newresult->nextresult=NULL;

    

    while (result->nextresult !=NULL)

        result = result->nextresult;

    

    result->nextresult = newresult;

    free(root);

}


/* print all the results in the linked list */


void PrintList (RESULT *result)

{

    if (result == NULL)

    return;

    

    printf("RESULT LIST REPORT FOR:EAST DEVON:");

    


    printf(" %s %s %s %08u %08u %08u %08u\n", result->party, result->lastName,

           result->firstName, result->votes[0], result->votes[1], result->votes[2], result->votes[3]);

    PrintList(result->nextresult);


}


I need to print all the results in the linkedList by order(e.g. by lastName), Could you direct me how to do it? Thank you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users