1

I have this error when try to open this code: An exception was thrown at address 0x7B7EE63C (ucrtbased.dll) in Struct.exe: 0xC0000005: access violation occurred while writing to address 0x0090000

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

struct automob {
    char marka[15];
    int year, price;
};

struct automob1 {
    char marka1[15];
    int year1, price1;
};


int main()
{
    FILE *file, *file1;
    struct automob a[5];

    fopen_s(&file,"file.txt", "w");

    for (int i = 0; i < 5; i++)
    {
        printf("Enter the mark: ");
        scanf_s("%s %d %d", a[i].marka, a[i].year, a[i].price);
        fprintf(file, "%s %d %d \n", a[i].marka, a[i].year, a[i].price);
    }

    fclose(file);

    fopen_s(&file, "C:\\Users\\tiama\\source\\repos\\Struct\Struct\\file.txt", "w");
    printf("Firm bla bla: \n");


    return 0;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • 3
    This is basically c code. Use `std::string`, it will make your life much easier. – cigien May 22 '20 at 02:46
  • @cigien I know, but I need to make it in C code – Alexei Petrenko May 22 '20 at 02:47
  • Then why have you tagged the post with c++? Go ahead, and remove that tag. – cigien May 22 '20 at 02:48
  • 2
    It looks like you need to read a C book or a manual to learn how to pass int variables to scanf. [Why scanf must take the address of operator](https://stackoverflow.com/questions/3893615/why-scanf-must-take-the-address-of-operator) – 273K May 22 '20 at 02:55
  • @S.M. I have changed to this ```scanf_s("%s %d %d", a[i].marka, &a[i].year, &a[i].price)``` but the problem still – Alexei Petrenko May 22 '20 at 03:01
  • 1
    what line does it crash on? Do your files open successfully? You don't check. Are you keeping the strings you enter 14 chars or less? I can never remember the order of operations, try `&(a[i].price)` and `&(a[i].year)`. no reason to define 2 structs with exactly the same fields. If you really want the same struct aliased with multiple names, just use `typedef` – yano May 22 '20 at 03:27
  • 1
    Don't be so stingy on those buffers. `char x[1024]`! – tadman May 22 '20 at 03:31
  • 5
    If you want this to be C, you need to remove `using namespace std;` – Tom Karzes May 22 '20 at 03:36
  • Note: `scanf()` expects to be passed pointers while `printf()` expects to be passed the actual data – user3629249 May 22 '20 at 05:26
  • regarding: `scanf_s("%s %d %d", a[i].marka, ....` The array `marka[]` has a length of 15 and `%s` always appends a NUL byte to the input, so to avoid a buffer overflow and the resulting undefined behavior, suggest: `scanf_s("%14s %d %d", a[i].marka, ...` – user3629249 May 22 '20 at 05:28
  • the function: `scanf_s()` returns the number of successful 'input format conversion' specifiers. So this statement: `scanf_s("%s %d %d", a[i].marka, a[i].year, a[i].price);` should be: `if( scanf_s("%s %d %d", a[i].marka, a[i].year, a[i].price) != 3) { fprintf( stderr, "scanf_s for one line of input failed\n" );` (cleanup by closing files, etc) then `exit( EXIT_FAILURE ); }` – user3629249 May 22 '20 at 05:34
  • regarding: `fopen_s(&file, "C:\\Users\\tiama\\source\\repos\\Struct\Struct\\file.txt", "w")` 1) why open it for writing when it is never used. 2) always check (!=NULL) the returned value to assure the operation was successful. If not successful (==NULL) then: `perror( "fopen_s failed" );` – user3629249 May 22 '20 at 05:38

0 Answers0