0x77B3FB53处的未处理异常

时间:2015-11-25 06:31:14

标签: c arrays

运行以下代码时出现以下错误。 我运行调试器,它似乎指向第77行。

Project.exe中出现未处理的win32异常[8704] Project.exe中0x77B3FB53(msvcr120d.dll)的未处理异常:0xC0000005:访问冲突读取位置0xCDCDCDCD。

Sample Source txt文件:

9001:0002:9003:0021:CLS
0001:0010:0003:0021:CLS
8001:0002:8002:0080:<HTML>
0001:4002:0002:0080:<BODY>
0004:0002:0002:0080:JHJKJBKHJBIUHBKBKHBKHHBKJBKJNKJKHHKUHKJLHBKHBKHBHBHBKHBHBHBHBBHHBHBJKJHKJHKJHKUHIUJ          

源代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>  
#include <string.h>
#include <stdlib.h> 
#include <malloc.h>

//struct definition
struct record{
int src;
int dest;
int type;
int port;
char data[100];
};


int main() {
struct record *array;

FILE* inFile; //file handle or pointer
FILE* outFile;
FILE* errorFile;
char filename[100];

int count = 0;  //keep track of number of records in memory
int i = 0;
int test;
int n1 = 0; //keep track number of correct records
int n2 = 0; //keep track number of error records

array = (struct record *) malloc(sizeof(struct record));

//User to enter filename
printf("Enter filename: ");
scanf("%s", filename);

printf("Processing filename %s ...\n", filename);
inFile = fopen(filename, "r");
if (inFile == NULL) //check if file handler is invalid {
    printf("Could not open file %s\n", filename);
    exit(1); //error status code
}

test = fscanf(inFile, "%d:%d:%d:%d",
    &array[count].src, &array[count].dest, &array[count].type, &array[count].port);
fgets(array[count].data, 100, inFile);
while (test != EOF){
    count++;
    array = (struct record *) realloc(array, (count + 1)*sizeof(struct record));
    test = fscanf(inFile, "%d:%d:%d:%d:%s",
        &array[count].src, &array[count].dest, &array[count].type, &array[count].port, &array[count].data);
}
fclose(inFile); //must always close file once done

outFile = fopen("data_1.txt", "wt");
errorFile = fopen("data_error.txt", "wt");
if (outFile == NULL) //check if file handler is invalid
{
    printf("Could not write to file \n", filename);
    exit(1);
}

if (count > 0){
    printf("Viewing all records: ", count);
    for (i = 0; i < count; i++){
        if (array[count].src >= 1 && array[count].src <= 1024 && array[count].dest >= 1 && array[count].dest <= 1024 && array[count].type >= 1 && array[count].type <= 10 && array[count].port >= 1 && array[count].port <= 1024 && strlen(array[count].data) >= 1 && strlen(array[count].data) <= 50)
            n1++;
            fprintf(outFile, "%d %d %d %d %s",
            (i + 1),
            array[count].src,
            array[count].dest,
            array[count].type,
            array[count].port,
            array[count].data
            );
    }
}
else
{
    n2++;
    fprintf(errorFile, "%d %d %d %d %s",
        (i + 1),
        array[count].src,
        array[count].dest,
        array[count].type,
        array[count].port,
        array[count].data
        );
}

fclose(errorFile);
fclose(outFile);
return 0;
}

1 个答案:

答案 0 :(得分:2)

 fprintf(outFile, "%d %d %d %d %s",
        (i + 1),
        array[count].src,
        array[count].dest,
        array[count].type,
        array[count].port,
        array[count].data
        );

这有5个格式说明符,但有6个项目(包括(i + 1)),因此fprintf尝试将数组[count] .port解释为字符串指针。