gpt4 book ai didi

c - scanf 函数无法正确扫描

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:20 25 4
gpt4 key购买 nike

这是代码:

StudentTree* readStudentFile(FILE* studentFile){
StudentTree *node=NULL;
StudentTree *tree=NULL;
int id;
char name[10]="\0";
char lastname[10]="\0";
int number;
int grade;

fscanf(studentFile,"%9d%10c%10c%4d%*c%3d",&id,name,lastname,&number,&grade);

不知何故,变量名获得了所有 20 个字符,而变量姓氏只得到第二个 10 个字符。我希望变量名称只获取前 10 个字符。

最佳答案

字符数组必须定义为

char name[11] = "\0";
char lastname[11] = "\0";

有足够的空间容纳 10 个字符和一个终止 NUL 字符。

您的情况可能是 lastname 紧跟在 name 之后在内存中,以便打印 name 打印 both 数组的字符(也许更多,直到找到 NUL 字符)。

关于c - scanf 函数无法正确扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23423348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com