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);

    }
}

Can you give a sample input/output session (a limited one)? Also, your variable declarations should all be at the top of a block (e.g., at the top of main()) in order to be standard.

Can you give a sample input/output session (a limited one)? Also, your variable declarations should all be at the top of a block (e.g., at the top of main()) in order to be standard.

Thanks, here is output, there is sample of input file on the bottom of the original post. I'm not sure if this is what was asked for, or if this is tagged right (I looked at directions).

/*This is from print out of the line and the switch cases the lines should be seperated into strings,where a colon is, one string per line, these print correctly. */

L25:vacant::F:F
vacant

F
F
L26:vacant::F:
L26
vacant

F

L27:vacant::F:
L27
vacant

F

..............................


/*This is from the printMall function it should print the fields as above, but only the seperated strings one per line. vacant(the name) is printing with the first field, and on L25 the last fields run together. These don't print correctly, and I don't know why. */

L25vacant
vacant

FFL26vacant
FL26vacant


L26vacant
vacant

F

L27vacant
vacant

F
...............................

You did fine with this posting. Sometimes it's nice to put something like your text there (sample output) into code tags to keep it neat. I've got to admit I really haven't much of an idea about the requirements or the goals of the program. Could you post the stores.data (if it's not too long)? I think you were trying to show me what the output is currently from those two functions but it would also be good to see what it is supposed to look like.

You did fine with this posting. Sometimes it's nice to put something like your text there (sample output) into code tags to keep it neat. I've got to admit I really haven't much of an idea about the requirements or the goals of the program. Could you post the stores.data (if it's not too long)? I think you were trying to show me what the output is currently from those two functions but it would also be good to see what it is supposed to look like.

I thought I put part of it on the bottom, not enough sleep I guess. Here's part of it. This is going to be part of a larger program if I can ever get it to work. This part should read in from the file (like below) and then populate the fields of the struct. There are currently print statements that will be commented out, now used for testing. So as the output above shows, I get the line from the file ok, it seperates the strings, and when I print each field as I populate them it works, but then when printing with the printMall function the output isn't the same. It should print each field seperately one line each. It should be 3 characters for storeId, Lxx, storeName up to 35 charcters (vacant if no name), 8 character storePhone, 1 character for storeCat1, 1 character for storeCat2 all strings seperated by colons in the input file. Thanks for taking a look.

L01:vacant:::
L02:vacant:::
L03:vacant:::
L04:vacant:::
L05:vacant:::
L06:vacant:::
L07:vacant:::
L08:vacant:::
L09:vacant:::
L10:vacant:::
L11:vacant:::
L12:vacant:::
L13:vacant:::
L14:vacant:::
L15:vacant:::
L16:vacant:::
L17:vacant:::
L18:vacant:::
L19:vacant:::
L20:vacant:::
L21:vacant:::
L22:vacant:::
L23:vacant:::
L24:vacant:::
L25:vacant::F:F
L26:vacant::F:
L27:vacant::F:
L28:vacant::F:



//output should look like
L25
vacant
                   //blank for no storePhone number field
F
F
L26
vacant
                    //blank no storePhone number 
F
                     //blank no storeCat2
L27
vacant



......etc

In the main function you call print on line 107.. Before that can you print the entire array of structs ??

Your storeId field is defined to be only 3 chars in the struct, whereas your values (strings) are also 3 chars and there is no space allocated for the '\0' (end of string) character. This results in end of string character overflowing into the next field in the switch statement case 0 and then it gets overwritten when the storeName is set in case 1. As a result when you print at the end in printMall() function the end of string character for storeId is already vanished thus combining the storeId with storeName. That's why you are seeing concatenated storeId and storeName.

Just change your struct to define the storeId to be 4 chars and your program should work. char storeId[4];

Thanks I changed the size of the struct field and it worked, just like magic. I really appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.