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++;
开发者_StackOverflow社区 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;
}
return *buf
returns (just) the first character in the buf, which is all that you are storing into the firstName
and lastName
arrays. The problem is that arrays aren't first-class types in C, so you can't pass them as arguments to functions or return them as return values.
What you probably want to do is pass pointer to the buffer to fill in to parseName
and have it fill it in rather than using a local buffer. Then you just pass in firstName
or lastName
directly, which become pointers to the beginning of those fields.
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.
In parseName
you're putting all the input into the buffer and returning the first character, then assigning that character to firstName[0]
or lastName[0]
and never doing anything with the rest of the data that you put in the buffer. No wonder only one character is being printed.
You should probably change your
grade[position].firstName[0] = parseName(buf, &used);
to
parseName(grade[position].firstName, &used);
There's not really any reason to return the first character in parseName
. You could change the return type of parseName
to char*
and return the buf
instead of *buf
for a little redundancy (some C functions do it) and that'd be better design.
精彩评论