Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:45:08+00:00 2026-06-04T15:45:08+00:00

The following code prints the desired output but it prints garbage at the end

  • 0

The following code prints the desired output but it prints garbage at the end of the string. There is something wrong with the last call to MultiByteToWideChar but I can’t figure out what. Please help??

#include "stdafx.h"
#include<Windows.h>
#include <iostream>
using namespace std;
#include<tchar.h>

int main( int, char *[] )
{
    TCHAR szPath[MAX_PATH];
    if(!GetModuleFileName(NULL,szPath,MAX_PATH))
    {cout<<"Unable to get module path"; exit(0);}

    char ansiStr[MAX_PATH];
    if(!WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,szPath,-1,
        ansiStr,MAX_PATH,NULL,NULL))
    {cout<<"Unicode to ANSI failed\n";
    cout<<GetLastError();exit(1);}

    string s(ansiStr);

    size_t pos = 0;

    while(1)
    {
        pos = s.find('\\',pos);
        if(pos == string::npos)
            break;
        s.insert(pos,1,'\\');
        pos+=2;
    }

    if(!MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,s.c_str(),s.size(),szPath,MAX_PATH))
    {cout<<"ANSI to Unicode failed"; exit(2);}

    wprintf(L"%s",szPath);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:45:09+00:00Added an answer on June 4, 2026 at 3:45 pm

    MSDN has this to say about the cbMultiByte parameter:

    If this parameter is -1, the function processes the entire input
    string, including the terminating null character. Therefore, the
    resulting Unicode string has a terminating null character, and the
    length returned by the function includes this character.

    If this parameter is set to a positive integer, the function processes
    exactly the specified number of bytes. If the provided size does not
    include a terminating null character, the resulting Unicode string is
    not null-terminated, and the returned length does not include this
    character.

    ..so if you want the output string to be 0 terminated you should include the 0 terminator in the length you pass in OR 0 terminate yourself based on the return value…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that generates the desired HTML. But it prints it
The following code, prints out Derived Base Base But I need every Derived object
The following code prints: generic overload But what I wanted is that the overload
The following code prints out the string T not the actual typename when the
The following code prints something like °Ð½Ð´Ð¸Ñ-ÐÑÐ¿Ð°Ð½Ð¸Ñ getDirectoryContents path/to/directory/that/contains/files/with/nonASCII/names >>= mapM_ putStrLn Looks like
The following code prints one , two , three . Is that desired and
I've written the following code, but I feel I'm going wrong somewhere: public class
The following code prints out 10 . How can I make it print out
The following code prints (when invoking MyMethod): 0 0 0 1 I would expect
The following code prints null once. class MyClass { private static MyClass myClass =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.