gpt4 book ai didi

c - 0x77B3FB53 处未处理的异常

转载 作者:行者123 更新时间:2023-11-30 20:12:53 25 4
gpt4 key购买 nike

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

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

示例源 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;
}

最佳答案

 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 尝试将 array[count].port 解释为字符串指针。

关于c - 0x77B3FB53 处未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com