Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct rec
{
    char nam[20];
    float grd;
    int id;
    struct rec *next;
};

struct rec head;
char xnam[20];
int xid;
float xgrd;


int read(struct rec happy[]);
void sort(struct rec sad[], int c );
int compare(char *ap, char *bp);
void swap( struct rec fish[], int w);

int main()
{
    struct rec what[20];
    int i=0, no=0;

    no = read(what);
    sort(what, no);

    getchar() ;
    return 0;
}


int read(struct rec info[])
{
    int l=0;

    FILE *myFile;
    myFile = fopen("student.txt", "r");


    printf("List Unsorted:\n");

    struct rec *tp;
    tp=&head;
    while (fscanf(head,"%s,%d,%f",xnam,&xgrd,&xid),myFile)
    {
        tp->next=struct rec malloc (sizeof struct rec);
        tp=tp->next;
        strcpy(tp->nam,xnam);
        tp->id=xid;
        tp->grd=xgrd;
        tp->next=NULL;
    }

    tp=head.next;
    while(head.next!=NULL)
    {
        printf("%s,%d,%f",tp->nam,tp->grd,tp->id);
    }
    fclose(myFile);

    return l;

}


int compare(char *ap, char *bp)
{
    int x = 0;
    while (ap[x] == bp[x] && ap[x] != '\0')
        x=x+1;

    if (ap[x] > bp[x])
        return 1;
    else if (ap[x] < bp[x])
        return -1;
    else
        return 0;
}


void swap( struct rec fish[], int w)
{
    int q;

    struct rec temp;

    q=compare (fish[w].nam, fish[w+1].nam);
    if (q>0)
    {
        temp = fish[w];
        fish[w] = fish[w+1];
        fish[w+1] = temp;
    }
}


void sort(struct rec cat[], int k)
{
    int y, z, c;

    for (y=0; y<k-1; y=y+1)
    {

        for (z=0; y+z<k-1; z=z+1)
        {
            swap(cat, z);
        }

    }
    printf("\nNew List\n\n");

    for (c=0; c<k; c=c+1)
    {
        printf("%s\t %d %.2f", cat[c].nam, cat[c].id, cat[c].grd);
        printf("\n");
    }
}


What I have tried:

i use debugger but nothing happen
Posted
Updated 9-Jan-18 9:45am
v2

1 solution

Quote:
tp->next=struct rec malloc (sizeof struct rec);
Should be
C
tp->next=(struct rec *) malloc (sizeof (struct rec));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900