This is homework, but I'm stumped on this problem. It prints store->storeId properly in "case 0", but storeId prints with the name added in the "printMall" function, i.e. "L01vacant" it then prints "vacant" for store->store name as it should and the rest of the printMall function prints properly. I've changed things back and forth in the switch function, but no dice.
Any help for the clueless would be greatly appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_STORES 52
#define STOREID_SIZE 3
typedef struct store
{
char storeId[3];
char storeName[35];
char storePhone[8];
char storeCat1[1];
char storeCat2[1];
}Store;
Store mall[MAX_STORES];
Store vacant[MAX_STORES];
void intializeMall(Store mall[]);
void intializeStore(Store *store);
void printMall(Store mall[]);
int main(void)
{
//intializeMall(mall);
int fieldCount;
int lineCount = -1;
static const char filename[] = "stores.data"; /* the name of a file to open */
FILE *file = fopen(filename, "r"); /* try to open the file */
if ( file != NULL )
{
char line[BUFSIZ]; /* space to read a line into */
/*
* Create an array of strings: here each line may be broken up into up
*/
char data[52][35];
while ( fgets(line, sizeof line, file) != NULL ) /* read each line */
{
lineCount ++;
Store *store = &mall[lineCount];
size_t i = 0;
// size_t size;
char *token = line;
fputs(line, stdout);
for ( fieldCount = 0 ;fieldCount < 4; fieldCount++ )
{
size_t len = strcspn(token, ":\n"); /* search for delimiters */
/*
* Use sprint to copy the text into a char array.
*/
switch (fieldCount)
{
case 0:
sprintf(store->storeId, "%.*s", (int)len, token);
printf("%s\n", store->storeId);
break;
case 1:
sprintf(store->storeName, "%.*s", (int)len, token);
printf("%s\n", store->storeName);
break;
case 2:
sprintf(store->storePhone, "%.*s", (int)len, token);
printf("%s\n", store->storePhone);
break;
case 3:
sprintf(store->storeCat1, "%.*s", (int)len, token);
printf("%s\n", store->storeCat1);
break;
case 4:
sprintf(store->storeCat2, "%.*s", (int)len, token);
printf("%s\n", store->storeCat2);
break;
default:
break;
}
token += len; /* advance pointer by the length of the found text */
if ( *token == '\0' )
{
break; /* advanced to the terminating null character */
}
++token; /* skip the delimiter */
/* advance the index into the string array */
if ( ++i >= sizeof data / sizeof *data )
{
puts("no more room");
break;
}
}
}
fclose(file);
}
else
{
perror(filename); /* why didn't the file open? */
}
printMall(mall);
//getch();
return 0;
}
void printMall(Store mall[])
{
int i = 0;
for (i=0; i<MAX_STORES; i++)
{
Store *store = &mall[i];
printf("%s\n", store->storeId);
printf("%s\n", store->storeName);
printf("%s\n", store->storePhone);
printf("%s\n", store->storeCat1);
printf("%s\n\n\n", store->storeCat2);
}
}