c - 异常未处理

标签 c exception

在 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/

相关文章:

c - 如何正确比较 C 中的字符串?

delphi - 如何在 EExternal 异常期间获取 EXCEPTION_POINTERS?

android - HttpHostConnectException 连接被拒绝 ConnectException ErrnoException

java - 有时连接 MySQL 时出现问题

python - 在 Python 中如何找到引发异常的位置

c - 语音比较算法

c - 尝试编写一个删除节点的函数

c - 意外的链接描述文件符号地址

c++ - 以安全的方式了解 pthread 线程是否处于事件状态

android - listenUsingRfcommWithServiceRecord(...) 返回 null