程序未成功结束 [英] Programs doesn't end successfully

查看:79
本文介绍了程序未成功结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了类似于以下内容的程序

i have made the program like the following

int main()
{
//create a socket
//connect to server
//send data to server
//recv data from server
//parse the data
IN the parse function "fill1.Total_hourmeter" get filled after parsing the data


//write parse the data into buffer
{
char buf_send[200];
    memset(buf_send,'\0', sizeof(buf_send));
strcpy(b_Total_hourmeter,fill1.Total_hourmeter);
    b_Total_hourmeter[8]='\0';

sprintf(buf_send,"%.2s",b_Total_hourmeter);
WriteToDataFile(buf_send);

}
#define BufferFilePath "C:\\Documents and Settings\\Administrator\\Desktop\\20sept_new\\20sept\\New_Nipro1\\NiproBuf.txt"
int WriteToDataFile(char* str)
{
    FILE* log;
    char    szDate[12];
        char    szTime[12];
    char buf_buf[2500];
    memset(buf_buf, 0, sizeof(buf_buf));
    _strdate( szDate );
        _strtime( szTime );
    sprintf(buf_buf,%s::%s:Info:%s, szDate, szTime, str);
    log = fopen(BufferFilePath,a+);
    if (log == NULL)
        return -1;
    fprintf(log,%s\n, buf_buf);
    fclose(log);
    return 0;
}

My function call is like this:-

struct a
{
char Total_hourmeter[8];
}fill1;
int main()
{
socket create
connect()/call
send call
recv call//
parse_recv_data(buffer)
{
//parsing

write_to_buffer()
return 0;
}

void write_to_buffer()
{
char b_Total_hourmeter[8];
char buf_send[200];
    memset(buf_send,'\0', sizeof(buf_send));
strcpy(b_Total_hourmeter,fill1.Total_hourmeter);
    b_Total_hourmeter[8]='\0';
WriteToDataFile(buf_send);

printf("");
}



现在的问题是,在将数据成功写入文件之后,我的代码在"printf(");处中断了"在"write_to_buffer()"函数中说运行时检查失败#2-围绕变量"b_Total_hourmeter"的堆栈已损坏.";
如果我做了这样的事情



Now the problem is after writing data to file successfully my code breaks at "printf("");" in "write_to_buffer()" function saying "Run-Time Check Failure #2 - Stack around the variable ''b_Total_hourmeter'' was corrupted.";
if i did something like this

void write_to_buffer()
{
....
WriteToDataFile(buf_send);
cin.ignore();
}


它工作正常,但我必须关闭控制台,程序在"return 0;"结束时不会自行终止.用任何程序编写的


It works fine but i have to close the console my self,the programs doesn''t ends itself as it ends when "return 0;" is written in any program

推荐答案

我认为您在以下位置产生了损坏的堆栈:

I think you make corrupt stack at:

b_Total_hourmeter[8]='\0';



您声明一个b_Total_hourmeter [8]缓冲区,该缓冲区的最大索引为7,而不是8.



You declare a b_Total_hourmeter[8] buffer, maximum index of buffer is 7, not 8.


这篇关于程序未成功结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆