为什么我的C代码会导致Visual Studio崩溃?

为什么我的C代码会导致Visual Studio崩溃?,c,visual-studio-2013,C,Visual Studio 2013,我正在尝试用C编写代码来计算文本文件中的单词(在本例中,文本文件有一个名称列表)。在它找到文本文件中的名称数之后,我想提示用户为每个名称输入一个分数(在0-100之间),然后计算一个平均分数。但是,每次我尝试运行我的程序时,它都会崩溃,并弹出: 下面是我试图编译的代码: /* 1. Read a list of names from a file called names.txt 2. Count the number of names in the list. 3. Prompt th

我正在尝试用C编写代码来计算文本文件中的单词(在本例中,文本文件有一个名称列表)。在它找到文本文件中的名称数之后,我想提示用户为每个名称输入一个分数(在0-100之间),然后计算一个平均分数。但是,每次我尝试运行我的程序时,它都会崩溃,并弹出:

下面是我试图编译的代码:

/*
1.  Read a list of names from a file called names.txt
2.  Count the number of names in the list.
3.  Prompt the user to provide a score (0 – 100) for each name in the file.
4.  Output the name and corresponding score to the file names_scores.txt
*/
#include <stdio.h>

#define IN 1//inside a word
#define OUT 0//outside a word

/*count lines, words, and characters in input*/
int main()
{
    int c;//storage variable for nw delimiters
    int nl;//new line
    int nw;//new word
    int nc;//next new character towards EOF 
    int nwCount;//number of words found in .txt file

    int state = OUT;//Indicates whether we're inside or outside of a word. We initialize
                    //it to being outside of the first word in the .txt file.

    nl = nw = nc = nwCount = 0;
    FILE *myFile = fopen_s("pfile", "names.txt", "r");
    if (myFile == NULL){
        perror("Error opening file.\n");
        return -1;
    }
    while ((c = _fgetchar()) != EOF){//let 'c' act as a storage variable for the nw delimiters

        ++nc;//increment the curent new character (do we need this incrementer?)
        if (c == ' ' || c == '\n' || c == '\t'){//nw delimiters
            state = OUT;
        }
        else {//if (state == OUT){
            state = IN;
            for (nwCount = 0; nwCount < 6; nwCount++){
                scanf_s("%d", &nw);
                nwCount += nw;
            }
        }   

    }//end of while loop

    //printf("%d %d %d\n", nl, nw, nc);//test the word counter

    int score = 0;
    int scoreQty = 0;
    int scoreSum = 0;
    int scoreAverage = 0;//scoreAverage = scoreSum / nwCount

    printf("There are ");
    printf("%d", nwCount);
    printf(" names in the names.txt file.\n");

    printf("Please enter a score integer (between 0 - 100) for each name.\n\n");
    for (scoreQty = 0; scoreQty < nwCount; scoreQty++){
        scanf_s("%d", &score);
        scoreSum += score;
    }

    printf("\nThe average score is: ");
    printf("%d %", scoreSum / scoreQty);
}
/*
1.从名为names.txt的文件中读取名称列表
2.计算列表中的姓名数。
3.提示用户为文件中的每个名称提供分数(0–100)。
4.将名称和相应的分数输出到文件名_scores.txt
*/
#包括
#在1//中定义一个单词
#在单词外定义0//
/*计算输入中的行、字和字符数*/
int main()
{
int c;//nw分隔符的存储变量
int nl;//新行
int nw;//新单词
int nc;//指向EOF的下一个新字符
int nwCount;//在.txt文件中找到的字数
int state=OUT;//指示我们是在单词内部还是外部。我们初始化
//它可能位于.txt文件的第一个单词之外。
nl=nw=nc=nwCount=0;
FILE*myFile=fopen_s(“pfile”、“names.txt”、“r”);
if(myFile==NULL){
perror(“打开文件时出错。\n”);
返回-1;
}
while((c=_fgetchar())!=EOF){//让'c'充当nw分隔符的存储变量
++nc;//递增当前的新字符(我们需要这个递增器吗?)
如果(c=''| | c='\n'| | c=='\t'){//nw分隔符
状态=输出;
}
else{//if(state==OUT){
状态=IN;
对于(nwCount=0;nwCount<6;nwCount++){
扫描频率(“%d”和“nw”);
nwCount+=nw;
}
}   
}//while循环结束
//printf(“%d%d%d\n”,nl,nw,nc);//测试单词计数器
智力得分=0;
int scoreQty=0;
int scoreSum=0;
int scoreAverage=0;//scoreAverage=scoreSum/nwCount
printf(“有”);
printf(“%d”,nwCount);
printf(“names.txt文件中的名称。\n”);
printf(“请为每个名称输入一个分数整数(0-100之间)。\n\n”);
对于(scoreQty=0;scoreQty

我使用Visual Studio 2013作为编译器。

崩溃的原因是调用
fopen_s
不正确

改变

FILE *myFile = fopen_s("pfile", "names.txt", "r");


请参见

调试器在帮助您查找此类崩溃问题方面非常出色。您是否尝试单击错误对话框中的“调试”按钮?看起来您并没有真正关闭IDE…无论是什么,
\u fgetchar()
,以及
scanf_s()
的混合看起来非常可疑。
scanf_s()
无法为您准备文件中的任何内容,除非您将文件重定向到标准输入
也不会从文件中读取任何内容。如果
scoreQty
为0,则在最后一行
printf
上有一个被0除的错误。最后一行还有一个错误的格式字符串。要打印“%”,必须使用“%%”。因此,程序具有未定义的行为。
errno_t err;
FILE *myFile;
err = fopen_s(&myFile, "names.txt", "r");