CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    43

    _mbsnbcnt() problems

    hi,everyone

    I want to use the _mbsnbcnt() to get the bytes number of a string

    my code is as follows:

    define CHAR_PER_LINE 30
    char *pBuf=new char[100];
    memset(pBuf,32,100);

    int my function:i first read in string from the txt file,and then use
    the following line:

    int nLen=_mbsnbcnt( (unsigned char*)pBuf,CHAR_PER_LINE);

    when i check the value of the nlen , i found that some times it larger than the CHAR_PER_LINE,why?

  2. #2
    Join Date
    Jul 2001
    Location
    Essen/Germany
    Posts
    57
    Looks like you miss the terminating zero at your stringĀ“s end. Maybe this helps:

    Code:
    #define CHARS_PER_LINE 30
    #define BUFFER_SIZE 100
    
    char *pBuf = new char[BUFFER_SIZE];
    if( pBuf ) {
      memset( pBuf, 0, BUFFER_SIZE );
      // read max. BUFFER_SIZE -1 characters into your buffer
    
      int nLen=_mbsnbcnt( (unsigned char*)pBuf,CHAR_PER_LINE);
    }
    Second, maybe the lines you read from your file are longer that 30 characters?

    Good Luck,
    Guido

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured