Question: This piece of code should create separate files with respect to the names of candidates entered by the user. The error is that this only
This piece of code should create separate files with respect to the names of candidates entered by the user. The error is that this only generates one file that is last entered by the user. This is an incomplete code for mini voting system using single linked list and file handling in C language . Kindly make changes and remove the error and also kindly complete the code according to the requirement. Its urgent!
CODE:
void nameOFcand(){
candNode=(struct candidate*)(malloc(sizeof(struct candidate)));
printf(" Enter the number of candidates for election :"); scanf("%d",&candNode->numberOfcand);
for(int i = 1;i <= candNode->numberOfcand; i++){ printf("Names %d:",i); scanf("%s",&candNode->nameOFcand);
}
candNode->link=NULL; // linking in the nodes if(candHead==NULL){ candHead=candNode; } else{ b=candHead; while(b->link!=NULL){ b=b->link; } b->link=candNode; }
writeCandFile();
} void writeCandFile(){
FILE *candFile, *INdcand; b=candHead; candFile=fopen("Candidates.txt","a+"); while(b!=NULL){ fprintf(candFile,"%s ",b->nameOFcand); //sprintf(nameOFcand,".text",i); strcat(b->nameOFcand,".txt"); INdcand= fopen(b->nameOFcand,"a+"); fprintf( INdcand,"%s ", candNode->nameOFcand ); b=b->link; fclose(INdcand); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
