c - 程序按预期工作,但打印结果后我收到错误消息

标签 c windows file printing

#include "stdafx.h"
#include "string.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
    FILE *fp1, *fp2;
    char ime[32], prezime[32]; char brojbodova[5];
    fp1 = fopen("ulaz.txt", "r");
    char linija[100];
    char linijacopy[100];
while(fgets(linija, sizeof(linija), fp1) != NULL)
    {
/* or this: while (!feof(fp1))
        {
            fgets(linija, sizeof(linija), (FILE*)fp1);
            rest of code*/

    strcpy(linijacopy, linija);
    strcpy(ime, strtok(linija , " "));
    strcpy(prezime, strtok(NULL, " "));
    strcpy(brojbodova, strtok(NULL, " "));
    int bbodova = atoi(brojbodova);
    if(bbodova <= 50)
    {
        printf("%s\n", linijacopy);

    }

   }
fclose(fp1);
    return 0;
}

当我构建这个解决方案时,我没有任何错误,但是当我运行这个程序时,在打印结果后,我收到以下 Windows 错误:

Console application has stopped working.

Windows can check online for a solution to the problem.

And there are three options:

-Check online...

-Close the program

-Debug the program

Problem details:

Problem signature: Problem Event Name: APPCRASH
Application Name: ConsoleApplication8.exe 
Application Version: 0.0.0.0 
Application Timestamp: 5834b1c7 
Fault Module Name: MSVCR110D.dll 
Fault Module Version: 11.0.50727.1 
Fault Module Timestamp: 5011aa23 
Exception Code: c0000005 
Exception Offset: 0008f7b3 
OS Version: 6.1.7601.2.1.0.256.1 
Locale ID: 9242 
Additional Information 1: 0a9e 
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 
Additional Information 3: 0a9e 
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

该程序是在VS 2012中编写的,操作系统是Windows 7 32 Enterprise。

最佳答案

你的问题是假设每个函数都返回有效的指针,这是错误的

  1. 您必须检查 fopen() 的返回值,否则您将取消引用 NULL 指针。

  2. 您必须检查 strtok() 的返回值,同样的问题 → NULL 指针取消引用。

关于c - 程序按预期工作,但打印结果后我收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40752012/

相关文章:

.net - HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\TimeZoneKeyName 已损坏?

string - 为什么用:w and :append still overwrite the file?打开

c - 忽略我不想复制到文本文件中的字符串

创建一个新进程来执行ls命令

c - C 中的 Runge Kutta 用于 Lorenz 方程

C - fscanf 读取错误

c - 使用 C 和程序集创建用户级线程的段错误

windows - 如何将一个字符串附加到单词列表中的每个单词并将新单词列表分配给变量?

windows - 如何使用 FileSystemObject.Size 可靠地确定文件夹大小?

c++ - 读取文件抛出异常