I'm trying to make a database that holds some datas about teams for now. The problem is that I cannot read the entered datas record by record. I want to explain it with just one example:
insert manunited,manchester,old_trafford,1878,black-rd to teams
insert chelsea,london,stamford_bridge,1905,blue-whte to teams
select colors,team_name,founding_date from teams
select prints on screen the specified information i.e. colors,stadium etc.
insert takes teams information for now.So, According to the select command, output should be followed:
black-rd manunited 1878
blue-whte chelsea 1905
But, I get
black-rd manunited 1878 blue-whte
I've been trying to analyse my error for hours. I cannot find the error(s) and mistake(s). Thank you for all appreciated answers. By the way, I haven't maden while loop yet to insert or select commands. I'm pondering to find the error(s).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char team_name[TEAM_NAME];
char city[TEAM_NAME];
char stadium[STADIUM_NAME];
int founding_date;
char colors[COLOR];
}teams;
int main()
{
char read_command[12];
/* checking insert */
char str1[100],str2[5],str3[18];
FILE *fptr;
teams t;
scanf("%s", read_command);
if(strcmp(read_command,"insert") == 0)
{
scanf("%s %s %s", str1, str2, str3);
//printf("%s\n%s\n%s",str1,str2,str3);
insertfunc(fptr,str1, str2, str3);
}
if(strcmp(read_command,"select") == 0)
{
scanf("%s %s %s", str1, str2, str3);
selectfunc(fptr,str1, str2, str3);
}
return 0;
}
void selectfunc(FILE *fptr,char *str1,char *str2,char *str3)
{
char *buff;
buff = (char*) malloc(strlen(str1) + 1);
strcpy(buff,str1);
char *token;
const char comma[2] = ",";
if(strcmp(str3,"teams") == 0)
{
teams t;
fptr = fopen("teams.bin","rb");
if( fptr == NULL )
{
perror("File cannot be opened.");
exit(1);
}
else
{
while(fread(&t,sizeof(teams),1,fptr) == 1)
{
/* get the first token */
token = strtok(buff, comma);
while( token != NULL )
{
if(strcmp(token,"team_name") == 0)
{
printf(" %s ",t.team_name);
}
else if(strcmp(token,"city") == 0)
{
printf(" %s ",t.city);
}
else if(strcmp(token,"stadium") == 0)
{
printf(" %s ",t.stadium);
}
else if(strcmp(token,"colors") == 0)
{
printf(" %s ",t.colors);
}
else
{
printf(" %d ",t.founding_date);
}
token = strtok(NULL, comma);
}
}
}
fclose(fptr);
}
}
void insertfunc(FILE *fptr,char *str1,char *str2,char *str3)
{
char *buff;
buff = (char*) malloc(strlen(str1) + 1);
strcpy(buff,str1);
const char comma[2] = ",";
/*
if(buff == NULL)
perror("error");
*/
if(strcmp(str3,"teams") == 0)
{
teams t;
int date;
strcpy(t.team_name,strtok(buff, comma));
strcpy(t.city, strtok(NULL, comma));
strcpy(t.stadium, strtok(NULL, comma));
date = atoi(strtok(NULL, comma));
t.founding_date = date;
strcpy(t.colors, strtok(NULL, comma));
fptr = fopen("teams.bin","ab+");
if( fptr == NULL )
{
perror("File cannot be opened.");
exit(1);
}
else
{
fwrite(&t,sizeof(teams),1,fptr);
fclose(fptr);
}
}
free(buff);
}
freadand stick to a text format and functions for this.fptrin without ever setting it. Its unrelated to your question, but it is none-the-less indeterminate. Even evaluating it invokes undefined behavior. That thing has no reason to even be a parameter from what I can see.strtok, you can't tokenize the same string twice (and you need to learn how to use a debugger).