Tag Archives: Run-Time Check Failure #2 – Stack around the variable ‘a’ was corrupted

[Solved] Run-Time Check Failure #2 – Stack around the variable ‘a’ was corrupted

 

The code is as follows.

// EOF example.cpp: Define the entry point of the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
  char ch;
  FILE *f;
  char a[300];
  printf("enter the file name:");
  scanf("%s", a);
  f = fopen(a,"r");


  if (f == NULL)
  {
    printf("failed to open file. ");
    exit(1);
  }
  while (( ch=getc(f)) != EOF)
  {
    putchar(ch);
  }
  fclose(f);
  getchar();
  return 0;
}

 

Solution: project property page (ALT + F7) – > Configuration properties – > C++ -> Code generation – > Basic runtime check defaults

But it can’t solve the problem, so I set the input. TXT file format to ANSI instead of UTF-8 or Unicode, which is correct

In addition, when using scanf and fopen functions, the solution is as follows

Right click the project name – > Attributes – > C/C++–> Preprocessor – > To define the preprocessor, edit the input box on the right and add:
to it_ CRT_ SECURE_ NO_ WARNINGS