Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio VC++
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 22:05pm
Edited 20-Sep-12 22:33pm
v2
Comments
Andrewpeter at 21-Sep-12 4:22am
   
You try to change cin.ignore() into fflush() or sync()?
Tarun Batra at 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 at 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
0 Sergey Alexandrovich Kryukov 480
1 mhegazy94 460
2 Ravi Bhavnani 190
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2014
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