Search code examples
cdata-structurescharacter-arrays

Only first character of string is will print in C


I have this struct

typedef struct grade
{
    char firstName[SIZE];
    char lastName[SIZE];
    int stuNum;
}GRADE;

and this output code:

void printData(GRADE grade[], int used)
{
    int i;
    for(i=0; i<used-1; i++)
    {
        printf("%s %s %d\n", grade[i].firstName, grade[i].lastName, grade[i].stuNum);
    }
}

When I attempt to print the char's they only print the first character of the string, I'm at a stand still and cannot really figure out how to make it print the rest of the string.

This code reads the data file (which is in the format of "Firstname Lastname StudentNumber" in about 17 lines, for example "Mark Markinson 1234" newline "blah blah 1234" etc).

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        grade[position].firstName[0] = parseName(buf, &used);
        used++;

        grade[position].lastName[0] = parseName(buf, &used);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

This is the code that parses the buffer for names:

char parseName(char *str, int *place)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;

    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    return *buf;
}

after taking some suggestions here i changed some of the code, but it still does the same thing (only first character is printed)

used this way to call the function

parseName(buf, &used, grade[position].firstName);

and parseName is now

void parseName(char *str, int *place, char *firstName)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;    
    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    *firstName = *buf;
}

Solution

  • The problem is here:

    void readData(FILE *infile, GRADE grade[], int *count)
    {
        char buf[SIZE] = {0};
        int position=*count;
        int used = 0;
        while( fgets(buf, SIZE, infile))
        { 
            removeNL(buf);
    
            grade[position].firstName[0] = parseName(buf, &used);
            used++;
    
            grade[position].lastName[0] = parseName(buf, &used);
            used++;
    
            grade[position].stuNum = parseNumber(buf, &used);
            used = 0;
            position++;
        }
        *count = position;
    
    }
    

    When you call parseName, you just set the first characters. You should pass your array (where you will write the string) to parseName:

    void readData(FILE *infile, GRADE grade[], int *count)
    {
        char buf[SIZE] = {0};
        int position=*count;
        int used = 0;
        while( fgets(buf, SIZE, infile))
        { 
            removeNL(buf);
    
            parseName(buf, &used, grade[position].firstName);
            used++;
    
            parseName(buf, &used, grade[position].lastName);
            used++;
    
            grade[position].stuNum = parseNumber(buf, &used);
            used = 0;
            position++;
        }
        *count = position;
    
    }
    

    Don't forget to change parseName adding a parameter and writing the result to it.