2013-03-01 211 views
0

我有Windows 8與vs2010,當我運行這部分程序時顯示此錯誤: 運行時檢查失敗#2 - 圍繞變量「seqA」已損壞。 ................................................. .............................運行時檢查失敗#2 - 圍繞變量'seqA'的堆棧損壞

#include <iostream.h> 
#include <stdio.h> 
#include <string> 
#include<stdlib.h> 
#include <fstream> 
using namespace std; 

    int main(){ 
int lenA = 0; 
int lenB = 0; 
FILE * fileA, * fileB; 
char holder; 
char seqA[10], seqB[10]; 

/*open first file*/ 
fileA=fopen("c:\\str1.fa", "r"); 

/*check to see if it opened okay*/ 
if(fileA == NULL) { 
    perror ("Error opening 'str1.fa'\n"); 
    exit(EXIT_FAILURE); 
} 


/*open second file*/ 
fileB = fopen("c:\\str2.fa", "r"); 

/*check to see if it opened okay*/ 
if(fileB == NULL) { 
    perror ("Error opening 'str1.fa'\n"); 
    exit(EXIT_FAILURE); 
} 

/*measure file1 length*/ 
while(fgetc(fileA) != EOF) { 
    holder = fgetc(fileA); 
    seqA[lenA]=holder; 
    lenA++; 
} 
lenA--; 

fclose(fileA); 

holder='0'; 

/*measure file2 length*/ 
while(fgetc(fileB) != EOF) { 
    holder = fgetc(fileB); 
    seqB[lenB]=holder; 
    lenB++; 
} 
lenB--; 

fclose(fileB); 

鏈接picture此錯誤。

+0

除非您的文件少於10個字節,否則您正在寫入已分配數組的末尾。 – 2013-03-01 20:07:24

回答

0

你的讀取文件的方法是不安全的你有一個變量是char [10]你的文件有10個以上的字符?你會在內存中使用seqA[10] = holder;(對於exe多少),順便說一句,你會覆蓋堆棧。我認爲這是問題。一個解決方案是在10個字符以後破解或使用動態分配的數組,並在達到其大小時重新分配它。

+0

這個解決方案的工作,我使用休息和這個錯誤沒有顯示:)謝謝。 – mahdimb 2013-03-01 20:49:27

0

此:seqB[lenA]=holder; lenA++;會溢出,除非你的文件是非常短(10個字符或更少(並同樣與seqBlenB

0

您初始化seqA和SeqB到10個字符,我認爲該文件是比我的大。你正在通過在序列數組之外寫入來破壞堆棧,爲序列分配更多的內存

相關問題