在 Visual Studio 中,fscanf 给我这个错误:
Programação Imperativa.exe 中 0x6080D4EC (ucrtbased.dll) 处未处理的异常:0xC0000005:访问冲突写入位置 0x00D0B000。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
struct estrutura
{
char nome[100];
float no1;
float no2;
float valor;
}v1, r1, r2, r3, r4;
int main(void)
{
FILE *circuito;
int x;
setlocale(LC_ALL, "Portuguese");
circuito = fopen("circuito.cir", "r");
if (circuito == NULL)
{
printf("Erro na abertura do ficheiro");
}
else
{
while ((x = fgetc(circuito)) != '\n');
fscanf_s(circuito, "%s %f %f %f\n", v1.nome, &v1.no1, &v1.no2, &v1.valor);
printf("%s %lf %lf %lf \n", v1.nome, v1.no1, v1.no2, v1.valor);
fscanf_s(circuito, "%s %f %f %f\n", r1.nome, &r1.no1, &r1.no2, &r1.valor);
printf("%s %f %f %f \n", r1.nome, r1.no1, r1.no2, r1.valor);
}
return 0;
}
最佳答案
您错误地使用了带有 %s
说明符的 fscanf_s
。
Unlike
fscanf
...fscanf_s
... requires the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.
因为传递的参数太少,为接受最终 %f
的值传递的地址是未定义的。
编译器应该警告您缺少缓冲区大小参数。
关于c - 异常未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53990609/