当我使用此代码时
char *openFile(const char file[1024]){
FILE *f = fopen(file, "r");
char c[1000];
char *d;
if (f!=NULL) {
if (fgets(c, 1000, f) !=NULL){
d = c;
}
else{
d = "No text";
}
}
else{
d="error";
printf("%s", d);
}
fclose(f);
return d;
}
例如,获取这么长的文本
fosndjfnsondfnsnkdfsjndfoweoingfoweljksdnflwkengdfwoensldkfwejlnfkdlskdfnlskdnflskdnflksndlfknslkdnflkndlknfslnlfjnlksdnjfnjwnejnfwenfnjwenlfodnakdoifnkleroglknerolkdfgnrkldsfgnlskdfgnlksdfglndlfkngkldnslkgnlkfdnkglnklsndfklnglfdlskgknllkdglksdfkkngkresoirigknlsdf
(这个文本没有任何意义,它只是一个测试)我返回char *d;
它输出这个
fosndjfnsondfnsnkdfsjndfoweoingfoweljksdnflwkengdfwoensldkfwejlnfkdlskdfnlskdnflskdnflksndlfknslkdnflkndlknfslnlfjnlksdnjfnjwnejnfwenfnjwenlfodnakdoifnkleroglknerolkdfgnrkldsfgnlskdfgnlksdfglndlfkngkldnslkgnlkfdnkglnklsndfklnglfdlskgknllkdglksdfkkngkresoir¯ˇˇˇˇˇˇˇv”[
为什么最后会有那些奇怪的角色?