Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have made the program like the following

C++
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("");
}


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
C++
void write_to_buffer()
{
....
WriteToDataFile(buf_send);
cin.ignore();
}

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
Posted
Updated 20-Sep-12 22:33pm
v2
Comments
Andrewpeter 21-Sep-12 4:22am    
You try to change cin.ignore() into fflush() or sync()?
Tarun Batra 21-Sep-12 4:28am    
sir my problem is if i use cin.ignore() program runs but doesn't terminate acc to convention parse_recv_data() call the write_to_buffer() function which in turn call the WriteToDataFile(buf_send); function so after writing to datafile function should return to write_to_buffer() function and then to parse_recv_data() function,but at the closing braces of write_to_buffer(){ "}" my code breaks saying "Run-Time Check Failure #2 - Stack around the variable 'b_Total_hourmeter' was corrupted." so if i use cin.ignore(); my code doesn't break but also doesn't terminate successfully
Andrewpeter 21-Sep-12 4:39am    
See my answer below!

1 solution

I think you make corrupt stack at:

C++
b_Total_hourmeter[8]='\0';


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900