Below is my code in c++
//The data below r fixed ie the data to be uploaded in the buffer is in this format :KL10<nnnnnn><ssss>
KL (is the ID) , 10 (the total length of n and s), n(6 digit integer), s(4 digit serial no)

case 1:
char InBuffer[64];
BOOL flag=0;
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);

case 2:
char InBuffer[64];
BOOL flag=1;
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);
memcpy(&InBuffer[4],"123456",6);
memcpy(InBuffer[10],"0987",4);

Now, i want to get the length of the buffer now.
I used strlen(InBuffer) and it returned me 21 instead of 14 for case 2:
i want a new line exactly in the 14th offset
ie InBuffer[14] = 0x0A;
so now including the new line i want the len of buffer has 15. I want this len value for further calculation.
But why I am getting 21 as strlen(InBuffer)? Where is my mistake?

My current solution for case 2:

char InBuffer[64];
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);
memcpy(&InBuffer[4],"123456",6);
memcpy(InBuffer[10],"0987",4);
InBuffer[14] = 0x0A;
memcpy(&InBuffer[15],"\0",1);
BOOL flag=1;

//When i give
int dlen = strlen(InBuffer);
InBuffer[dlen] = 0x0A;
( the new line in added exactly in the 4th offset for case 1 but the same fails for case 2 )
(so i introd boolean var, which will bypass InBuffer[dlen] = 0x0A for case 2 since have already added the 0A and \0)
if (flag==1)
//i hardcode the buffer length as 15.

Looking for solution that will return the exact length of buffer for case 2.
Thanks.

Anu

Recommended Answers

All 14 Replies

You've got:

    memcpy(InBuffer,"KL10",4);
    memcpy(&InBuffer[4],"123456",6);
    memcpy(InBuffer[10],"0987",4);

Do you see the error in line 3?

sorry that was a typo error here but the code is perfect as below.
memcpy(&InBuffer[10],"0987",4); //this

If you really have typed line 3 (in reference to nullptr's post) that way, it is sure to generate a compile-time error cannot convert parameter 1 from 'char' to 'void *'. But if you have typed it like line 2, I'm pretty sure you will get a 14.

What compiler are you using?

char InBuffer[64];  // char InBuffer[64] = {0};
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);
memcpy(&InBuffer[4],"123456",6);
memcpy(&InBuffer[10],"0987",4);

unsigned __int32 len = strlen(InBuffer); // I get len = 14

Edit, I just noticed you have:

case 1:
char InBuffer[64];
BOOL flag=0;
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);

case 2:
char InBuffer[64];
BOOL flag=1;
memset(InBuffer,0,64);
memcpy(InBuffer,"KL10",4);
memcpy(&InBuffer[4],"123456",6);
memcpy(&InBuffer[10],"0987",4);

When declaring variables in a switch you need to use braces:

case 1:
{
    char InBuffer[64];
    BOOL flag=0;
    memset(InBuffer,0,64);
    memcpy(InBuffer,"KL10",4);
    break;   // so it doesn't keep dropping through all the cases
}

"When declaring variables in a switch you need to use braces:"
I have included these in d code and the syntax is upto d mark.
I had listed only d base functions happening in each case stmt and no problem in syntax.

I used unsigned __int32 len = strlen(InBuffer); **but i dont get 14.**

the buffer is as below
InBuffer[0] = K;
....
InBuffer[13] = 7;

leng = strlen(InBuffer); //this returns 21 and not 14.

now without hard coding these 2 lines
InBuffer[14] = 0x0A;
memcpy(&InBuffer[15],"\0",1);

Is it possible
1. To get the leng = strlen(InBuffer) as 14?
2. Then pad the InBuffer[leng] = 0x0A ?
3. Then get the final InBuffer length as 15?

sorry if am confusing u

Anu

It would likely be worth posting more of your code rather than just the snippets we see. Otherwise all I can do is guess that it's probably a scope problem with where the variables are declared.

commented: Thanks..ok give me some time, will post it +0

[

void CAbtDlg::OnBnClickedEnter()
{   
    int Data_Length;
    BOOL flag=0;
    BOOL bcheck=FALSE;    
    FileName.open("Credit.txt");                    
    while (!bcheck)//read all line from file
    {
        getline(FileName, getcmd);
        memset(InBuffer, 0, 64; 
        string subString_is = getcmd.substr(0,3);//Extract PM00 from txt
    memcpy(InBuffer,subString._isc_str(),subString_is.size());//PM00 is copied

        switch (InBuffer[0]) 
        {
      case 'K': 
       if (InBuffer[1] == 'L')
         {
           memcpy(&InBuffer[4],"123456",6);            
           memcpy(&In[10],"0987",4);        
           flag = 1;
           InBuffer[14] = 0x0A;                
           memcpy(&InBuffer[15],"\0",1);                  
         }
       break;
      default:
      break;
         }//end of switch loop      

         Data_Length = strlen(OutBuffer);    
     if(flag==0)
        InBuffer[Data_Length] = 0x0A;


      //send the InBuffer to the client     
    }
    if (bcheck)
        FileName.close();}

]

Credit.txt file has these fixed data
------------------------------------
PM00
KL10

1. Initial PM00 is extracted and since the switch case fails, Data_Length return is 4 and able to send data to the client
2. The next line in KL10 and the InBuffer contains KL101234560987.
3. The Data_Length now return 15 becos of the last 3 lines added under case 'k':
    if these 3 lines r removed and the the Data_Length now i get is  21.
    y does it return 21?

Based on your code, you are expecting characters 'K' and 'L' that when met, you will copy two groups of character which lengths are 6 and 4, respectively. What if these conditions are not satisfied, do you have a handler to this?

One more thing, I suggest using strcpy than memcpy since the former automatically adds a terminating null character.

BTW, where is your OutBuffer? How was it initialized and, if ever, modified later? This could be the culprit.
string subString_is = getcmd.substr(0,3);//Extract PM00 from txt Are you sure you got 'PM00' when you get a subtring which length is only 3?
InBuffer[14] = 0x0A ??? This will overwrite the 15th character of your InBuffer. Is this intended?
memcpy(&InBuffer[15],"\0",1) This is not necessary. InBuffer[15 would suffice. If you use strcpy, this line will not even be necessary.

-----------------------------------------

Hello, I did not want to make this a new post. I tried to edit my previous post but I didn't know how to update it, as in, I didn't find a way. :(

commented: got a temporary soln to it... thanks a lot for the post.. +0

I just threw this together for testing purposes:

using namespace std;

void ReadtheFile();

string getcmd;
ifstream FileName;
char InBuffer[64];

int _tmain(int argc, _TCHAR* argv[])
{
    ReadtheFile();
    getchar();

    return 0;
}

void ReadtheFile()
{   
    size_t Data_Length;
    FileName.open("Credit.txt");
    if (FileName.fail() )
    {
        cout << "error opening file" << endl;
        return;
    }

    while (getline(FileName, getcmd) )
    {
        if (getcmd.empty() )
        {
            continue;
        }

        memset(InBuffer, 0, 64); 
        string subString_is = getcmd.substr(0, 4); // read length 4
        memcpy(InBuffer, subString_is.c_str(), subString_is.size() );

        switch (InBuffer[0]) 
        {
        case 'K': 
            if (InBuffer[1] == 'L')
            {
                memcpy(&InBuffer[4], "123456", 6);            
                memcpy(&InBuffer[10], "0987", 4);                         
            }
            break;

        default:
            break;
        }

        Data_Length = strlen(InBuffer);    
        cout << string(InBuffer) << " length: " << Data_Length << endl;             
    }
    FileName.close();
}


Credit.txt
-------------------------
KL10
PM00  

KL10
CRAP

-------------------------

Output:

KL101234560987 length: 14
PM00 length: 4
KL101234560987 length: 14
CRAP length: 4
commented: Hi, will change my code as per ur design and will get back to u. +0

Dear friends,

thanks for the valuable feedback and time.
however i found a temp. soln for the above and all seems to be fine.
now when i convert this mfc code into release mode, am not able to run into another pc.
The exe doesnt open.
i dont prefer to install vstudio in remote pc. It should be compiler independent.
Is it possible ?
Using visual studio 2012 c++
Thanks

anu

Hi

the mfc code now works in any pc and was able to solve the prob.
Since the application dont need the Visual Studio, i fixed the file path at my end and all functionalities back to track.
Thanks nullptr ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.