Array of strings question
Hey all, I'm working on a homework assignment that reads binary files and performs queries on them. I came across a problem with trying to not print out duplicate results that were read in from a binary file. I'm using fread() to get the name of a person then i want to store that name in an array of strings so when i go to print i can check to make sure it doesnt match any of the other names. The problem im having is that the array is filling up with pointers that all point to the last object read in. Heres my code.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int projcmd(char sPtr[])
{
FILE *finp;
FILE *finpdat;
char *relname;
char *filename;
char *attrname;
char s[] = "S";
char is[] = "I";
char project_name;
char eof_test;
int i,j,val, s_length, pos, atr_length, seek_val, tot_length, advance_val;
char *sori, *cslen, *sori_F;
char *attname;
char sch_string[80];
char os[80];
char test_string[80];
char **dupArray;
char temp[80];
char *reference;
char *tPtr;
tPtr = strtok(sPtr, " ");
tPtr = strtok(NULL, " ");
relname = tPtr;
tot_length = 0;
atr_length = 0;
seek_val = 0;
advance_val = 0;
attrname = strtok(NULL, " ");
j = 0;
while(attrname[j] != '\n')
j++;
attrname[j] = '\0';
printf("%s %s\n", relname, attrname);
strcat(relname,".sch");
printf("%s\n", relname);
if((finp = fopen(relname, "r")) == NULL)
{
printf("error");
exit(1);
}
fscanf(finp, "%d", &val);
pos = ftell(finp);
for(i=0;i<val;i++)
{
fscanf(finp, "%s", attname);
fscanf(finp, "\t%c", &sori);
fscanf(finp, "\t%d", &s_length);
tot_length = tot_length + s_length;
if((strcmp(attname, attrname))==0)
{
atr_length = s_length;
seek_val = tot_length - s_length;
sori_F = sori;
}
}
advance_val = tot_length - atr_length;
j=0;
while(relname[j]!= '.')
j++;
relname[j] = '\0';
fread(test_string,1,atr_length, finpdat);
strcpy(temp,test_string);
File Edit Options Buffers Tools C Help
strcat(relname, ".dat");
finpdat = fopen(relname, "r");
fseek(finpdat, (seek_val), SEEK_CUR);
j = 0;
dupArray = (char**)calloc(sizeof(char),5); // The strings print correctly
while((fread(&eof_test, sizeof(char), 1, finpdat))!= NULL)// so i know the error is so
{ // somewhere down here
fseek(finpdat, -1, SEEK_CUR);
fread(test_string,1,atr_length, finpdat);
strcpy(temp,test_string);
dupArray[j] = temp;
j++;
printf("%s\n",temp);
fseek(finpdat,advance_val,SEEK_CUR);
}
for(i=0;i<j;i++)
{
printf("%s\n", dupArray[i]);
}
}
I should mention down here that the correct results print to stdout just not to the array