只有字符串的第一个字符才会在 C 中打印
我有这个结构
typedef struct grade
{
char firstName[SIZE];
char lastName[SIZE];
int stuNum;
}GRADE;
和这个输出代码:
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);
}
}
当我尝试打印字符时,它们只打印字符串的第一个字符,我处于静止状态,无法真正弄清楚如何让它打印字符串的其余部分。
此代码读取数据文件(格式为 "Firstname Lastname StudentNumber"
,大约 17 行,例如 "Mark Markinson 1234"
newline
代码> <代码>“等等1234”等)。
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;
}
这是解析缓冲区中名称的代码:
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;
}
在此处采取一些建议后,我更改了一些代码,但它仍然执行相同的操作(仅打印第一个字符)
,使用这种方式调用函数
parseName(buf, &used, grade[position].firstName);
,并且 parseName 现在是
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
return *buf
返回(仅)buf 中的第一个字符,这是您存储到firstName
和lastName
数组中的所有字符。问题是数组不是 C 中的一流类型,因此您不能将它们作为参数传递给函数或将它们作为返回值返回。您可能想要做的是将指针传递给缓冲区以填充
parseName
并让它填充它,而不是使用本地缓冲区。然后,您只需直接传入firstName
或lastName
即可,它们将成为指向这些字段开头的指针。return *buf
returns (just) the first character in the buf, which is all that you are storing into thefirstName
andlastName
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 infirstName
orlastName
directly, which become pointers to the beginning of those fields.问题就在这里:
当你调用parseName时,你只是设置了第一个字符。您应该将数组(您将在其中写入字符串)传递给 parseName:
不要忘记更改 parseName 添加参数并将结果写入其中。
The problem is here:
When you call parseName, you just set the first characters. You should pass your array (where you will write the string) to parseName:
Don't forget to change parseName adding a parameter and writing the result to it.
在
parseName
中,您将所有输入放入缓冲区并返回第一个字符,然后将该字符分配给firstName[0]
或lastName[0]< /code> 并且永远不会对放入缓冲区的其余数据执行任何操作。难怪只打印一个字符。
您可能应该将您的更改更改
为
There's not 事实上任何理由返回
parseName
中的第一个字符。您可以将parseName
的返回类型更改为char*
并返回buf
而不是*buf
一点冗余(一些 C 函数可以做到这一点),这将是更好的设计。In
parseName
you're putting all the input into the buffer and returning the first character, then assigning that character tofirstName[0]
orlastName[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
to
There's not really any reason to return the first character in
parseName
. You could change the return type ofparseName
tochar*
and return thebuf
instead of*buf
for a little redundancy (some C functions do it) and that'd be better design.