!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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作