Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted


 

代碼如下  :

// EOF范例.cpp: 定義控制台應用程序的入口點。
//

#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;
}

 

 

解決方法:項目屬性頁(Alt+F7) -> 配置屬性 -> C++ -> 代碼生成 -> 基本運行時檢查 默認值

但是也不能解決 疑問了  我就將輸入的.txt文件  格式設置為 ANSI  而不是utf-8 或unicode  這時就正確了     

 

另外在使用scanf 與fopen 函數時報錯 解決方法如下

右鍵工程名–>屬性–>C/C++–>預處理器–>預處理器定義,編輯右邊輸入框加入: 
_CRT_SECURE_NO_WARNINGS 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM