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

  • SEARCH
  • Home
  • 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 6995491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:01:57+00:00 2026-05-27T20:01:57+00:00

I am trying to finalize my logging class. I have written it from scratch

  • 0

I am trying to finalize my logging class. I have written it from scratch and do not wish to use an alternative library of any kind. My problem lies within the fact that my logger has trouble outputting std::strings and only works when I denote it with the string.c_str() function.

Here is my logfile output function:

void Log::writeSuccess(char * text,...)
{
    // Grab the variables and insert them
    va_list ap;
    va_start(ap, text);
    char buff[BUFFER_SIZE];
    vsnprintf(buff, sizeof(buff), text, ap);

    // Output to the log
    logfile << "<-!-> " << buff << endl;
}

Here is a sample call to my log class object (ignore the uselessness of the call):

string test("This is a test string!");
errorLog.writeSuccess("Output: %s", test);

I end up with random characters and garbled output.

However, when I append the string, test with .c_str(), it outputs the text correctly.

The whole reason I am trying to avoid cstrings is because I understand they are not cross platform and am developing my client to support all the major operating systems.

To summarize:

  1. What is wrong with my log output function? Do you see any way it could be improved?

  2. Should I generally avoid c_strings?

  • 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-05-27T20:01:58+00:00Added an answer on May 27, 2026 at 8:01 pm

    You’re getting random garble when passing an std::string to vsnprintf because the format specifier "%s" is that of a C-string – a char*.

    std::string is not of type char*, but std::string.c_str() is of type char*. vsnprintf will basically read chars pointed to by the address that it presumes is that of a start of a C-string, up until the NUL character '\0'.

    An std::string pushed onto the stack and passed as an argument to vsnprintf is not a pointer to a char, however vsnprintf will just treat these bytes as an address and start reading chars/bytes from this address, causing undefined behaviour.

    The printf family of functions are not typesafe, since they rely on a format string and variable argument list, which is why your code will compile but you’ll get unexpected results.

    Bottom line is the printf family of functions expect a char* when you use the format specifier "%s".

    I also think you’re confusing C style strings (char[]) with the Microsoft-specific CString class. C style strings won’t cause you problems on different platforms at all; the literal "This is a test string!" is a C style string (const char[]).

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

Sidebar

Related Questions

I'm trying to use sqlite in a 'data services' class like below. I keep
I'm trying to select data from the database and I have the following code
I'm thinking about trying MongoDB to use for storing our stats but have some
I'm trying to figure out how to best use Sessions in (N)Hibernate. I have
I am trying get the data from my database but i am not getting
I have an SQLite database in my iOS application. I'm trying to use the
background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions
I'm trying to write a finalizer for Python classes that have circular references. I
I'm trying to finalize my design of the data model for my project, and
I'm trying to finalize my webpage's shopping cart and I'm running into one final

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.