In Visual Studio the fscanf gives me this error:
Unhandled exception at 0x6080D4EC (ucrtbased.dll) in Programação Imperativa.exe: 0xC0000005: Access violation writing location 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;
}
You are using fscanf_s
with %s
specifier incorrectly.
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.
Because there is one too few arguments passed, the address passed to accept a value for the final %f
is undefined.
The compiler should have warned you about the missing buffer size argument.
User contributions licensed under CC BY-SA 3.0