Commit 4afa4bca by Raul.Leemet

Add new file

parent 11cc75a4
Showing with 76 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 255
typedef struct
{
int GameID;
char PlayerName[LEN];
char KilledName[LEN];
}action;
action *ReadFromFile(int *Total, char *FileName);
void PrintStruct(int total,action *kill);
int main(void)
{
int PlayersTotal;
char FileName[]={"frags.txt"};
action *kill=ReadFromFile(&PlayersTotal,FileName);
PrintStruct(PlayersTotal,kill);
free(kill);
return 0;
}
action *ReadFromFile(int *Total, char *FileName)
{
int i=0;
action buf;
action *pTemp=NULL;
action *pArr=NULL;
FILE *fi=fopen(FileName,"r");
if(fi==NULL)
{
exit(1);
}
while(fscanf(fi,"%d%s%s",&buf.GameID,buf.PlayerName,buf.KilledName)!=EOF)
{
pTemp=(action*)realloc(pArr,sizeof(action)*(i+1));
if( pTemp == NULL)
{
printf("Memory Allocation Failed");
fclose(fi);
free(pArr);
exit(1);
}
else
{
pArr=pTemp;
(pArr+i)->GameID=buf.GameID;
strcpy((pArr + i)-> PlayerName,buf.PlayerName);
strcpy((pArr + i)-> KilledName,buf.KilledName);
i++;
}
}
printf("%d\n",i);
fclose(fi);
*Total=i;
return pArr;
}
void PrintStruct(int total,action *kill)
{
int i;
puts("Data:");
for(i = 0; i < total; i++)
{
printf("%d %s %s\n", (kill + i)->GameID,(kill+ i)->PlayerName, (kill + i)->KilledName);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment