!283 2021-12-23 16:18 采纳率: 50%
浏览 33
已结题

一段程序中,cin为什么会解决这个问题,而scanf为什么会引起错误!

这是我的这一段代码,当scanf时

img

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct {    
char name[20];    
int wuwen;    
int shuxue;    
int yingyu;    
int number;
}student;
typedef struct listnode {    
student data;    
struct listnode* next;
}listnode, * linklist;
linklist l = NULL;
FILE* fp;
char filename[100];
void createlist() {    
l = (listnode*)malloc(sizeof(listnode));
l->next = NULL;}
void openfile() {    
createlist();    
listnode* p, * q;    
q = l;    
while (true) {        
printf("\n\n\t\t\t请输入文件名:");        

scanf("%s", &filename);        
if ((fp = fopen(filename, "r")) == NULL) {        
    printf("\t\t\t没有该文件,需要创建s\n");    
        char ch; scanf("%c",&ch);            
if (ch == 's') {                

if ((fp = fopen(filename, "a+")) == NULL) {                    
printf("\t\t\t创建文件失败\n");    
            }                
else break;            
}            
else continue;        
}        
else break;    
}    
while (true) {        
p = (listnode*)malloc(sizeof(listnode));    
if (fscanf(fp, "%d\t%s\t%d\t%d\t%d", &p->data.number, &p->data.name, &p->data.wuwen, &p->data.shuxue, &p->data.yingyu) == EOF) {            
free(p);            
printf("\t\t\t读入成功\n");            
system("pause");            
break;        
}        
p->next = NULL;        
q->next = p;        
q = q->next;    
}    
fclose(fp);
}
int main() {    
openfile();
}
当为cin时,就可以解决这个问题

img

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct {    
char name[20];    
int wuwen;    
int shuxue;    
int yingyu;    
int number;
}student;
typedef struct listnode {    
student data;    
struct listnode* next;
}listnode, * linklist;
linklist l = NULL;
FILE* fp;
char filename[100];
void createlist() {    
l = (listnode*)malloc(sizeof(listnode));
l->next = NULL;}
void openfile() {    
createlist();    
listnode* p, * q;    
q = l;    
while (true) {        
printf("\n\n\t\t\t请输入文件名:");        

scanf("%s", &filename);        
if ((fp = fopen(filename, "r")) == NULL) {        
    printf("\t\t\t没有该文件,需要创建s\n");    
        char ch; cin >> ch;            
if (ch == 's') {                

if ((fp = fopen(filename, "a+")) == NULL) {                    
printf("\t\t\t创建文件失败\n");    
            }                
else break;            
}            
else continue;        
}        
else break;    
}    
while (true) {        
p = (listnode*)malloc(sizeof(listnode));    
if (fscanf(fp, "%d\t%s\t%d\t%d\t%d", &p->data.number, &p->data.name, &p->data.wuwen, &p->data.shuxue, &p->data.yingyu) == EOF) {            
free(p);            
printf("\t\t\t读入成功\n");            
system("pause");            
break;        
}        
p->next = NULL;        
q->next = p;        
q = q->next;    
}    
fclose(fp);
}
int main() {    
openfile();
}


所以,我就是想知道,为什么scanf会引起这样的错误

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2021-12-23 16:20
    关注

    scanf如果输入字符,会接收换行符的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私