Click here to Skip to main content
12,632,326 members (29,986 online)
Rate this:
 
Please Sign up or sign in to vote.
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("");
}


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

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 20-Sep-12 23:05pm
Updated 20-Sep-12 23: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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I think you make corrupt stack at:

b_Total_hourmeter[8]='\0';


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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100