Sunday, 29 September 2013

Error in Fscanf

Error in Fscanf

My code is a singly-linked that uses typedef struct which is in the form
of an array.
The program will ask the user for the text file to read, then the program
uses fscanf to store the data of the text file into the respective
variables.
The code contains while(!feof(file pointer))
The file of the user is in the format of
name(with spaces)
name2(with spaces)
number1
number2
.... until number10. And there are multiple "sets" of data with that
format within the file whose numbers of sets I do not know if.
I used *mygets for the names, (mygets = function which is basically fgets
that eliminate creation of newline.) then the rest, I used fscanf without
encountering any system error.
Now, I created a sample text file just to see if my codes are functioning
correctly.
The sample text file contains 2 "sets" of the data following the structure
mentioned earlier.
The problem is: The program reads and prints the First set of the data to
their respective variables correctly, but when the program prints the
Second set, it prints the First set again as if the Second set is ignored.
Code below:
void load_hero(hero *curr[])
{
char filename[256];
int totalwarrior;
hero *head;
hero *tail;
FILE *fwarrior;
head = NULL;
tail = NULL;
totalwarrior = 0;
printf("Name of Roster File for Warriors?(without .extension): \n");
scanf("%s",&filename);
strcat(filename,".txt");
fwarrior = fopen(filename,"r");
while(!feof(fwarrior))
{
curr[totalwarrior] = malloc(sizeof(hero));
curr[totalwarrior+1] = malloc(sizeof(hero));
mygets(curr[totalwarrior]->name,30,fwarrior);
mygets(curr[totalwarrior]->title,30,fwarrior);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->encoding);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->startstr);
fscanf(fwarrior,"%lf\n",&curr[totalwarrior]->incstr);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->startdex);
fscanf(fwarrior,"%lf\n",&curr[totalwarrior]->incdex);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->startintel);
fscanf(fwarrior,"%lf\n",&curr[totalwarrior]->incintel);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->basemindmg);
fscanf(fwarrior,"%d\n",&curr[totalwarrior]->basemaxdmg);
fscanf(fwarrior,"%lf\n",&curr[totalwarrior]->bat);
fscanf(fwarrior,"%lf\n",&curr[totalwarrior]->basearmor);
if (head == NULL)
{
head = curr[totalwarrior];
}
else
{
tail->next=curr[totalwarrior];
}
tail = curr[totalwarrior];
if (head!=NULL)
{
curr[totalwarrior] = head;
do {
printf("\n ::%s::Name\n",curr[totalwarrior]->name);
printf("\n ::%s::Title\n",curr[totalwarrior]->title);
printf("\n ::%d::Encoding\n",curr[totalwarrior]->encoding);
printf("\n ::%.d::Starting
Strength\n",curr[totalwarrior]->startstr);
printf("\n ::%.1lf::Increasing
Strength\n",curr[totalwarrior]->incstr);
printf("\n ::%.d::Starting
Dexterity\n",curr[totalwarrior]->startdex);
printf("\n ::%.1lf::Increasing
Dexterity\n",curr[totalwarrior]->incdex);
printf("\n ::%d::Starting
Intelligence\n",curr[totalwarrior]->startintel);
printf("\n ::%.1lf::Increasing
Intelligence\n",curr[totalwarrior]->incintel);
printf("\n ::%d::Base Minimum
Damage\n",curr[totalwarrior]->basemindmg);
printf("\n ::%d::Base Maximum
Damage\n",curr[totalwarrior]->basemaxdmg);
printf("\n ::%.1lf::Base Attack Time\n",curr[totalwarrior]->bat);
printf("\n ::%.1lf::Base Armor\n",curr[totalwarrior]->basearmor);
curr[totalwarrior] = curr[totalwarrior]->next =
curr[totalwarrior+1];
curr[totalwarrior] = NULL;
printf("Before Total %d\n",totalwarrior);
totalwarrior++;
printf("Now Total %d\n",totalwarrior);
}while(curr[totalwarrior-1] != NULL);
} free(curr[totalwarrior]);
}
}

No comments:

Post a Comment