这是代码:
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/