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 7572107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:50:30+00:00 2026-05-30T15:50:30+00:00

I have been converting some code from C++ to C#. My lack of understanding

  • 0

I have been converting some code from C++ to C#. My lack of understanding of C# API doesnt let me find the equivalent of fprintf. What Im basically trying to do is write a helper class to Log information to a file. So far I have go the following class defined. If someone see something unusual please let me know. The method “Log” only logs strings currently. I dont know if this is the best way to do this. Anyway I would like to convert some numbers to dump into the log file. In C++, I have fprintf doing the conversion. How can I achieve something similar in C# ??

fprintf(file, "Wheel1: %f \t Wheel2: %f \t Dist: %f, Wheel0, Wheel1, TotalDist);

public class Logger
{
    private string strPathName = string.Empty;
    private StreamWriter sw = null;

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="prefix"></param>
    public Logger(string prefix)
    {
        DateTime datet = DateTime.Now;

        // Format string
        if (string.IsNullOrEmpty(prefix))
        {
            prefix += "_";
        }
        else
        {
            prefix = "";
        }

        strPathName = "Log_" + prefix + datet.ToString("MM_dd_hhmmss") + ".log";
        if (File.Exists(strPathName) == true)
        {
            FileStream fs = new FileStream(strPathName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fs.Close();
        }
    }

    /// <summary>
    /// Create a directory if not exists
    /// </summary>
    /// <param name="strLogPath"></param>
    /// <returns></returns>
    private bool CheckDirectory(string strLogPath)
    {
        try
        {
            int nFindSlashPos = strLogPath.Trim().LastIndexOf("\\");
            string strDirectoryname = strLogPath.Trim().Substring(0, nFindSlashPos);

            if (Directory.Exists(strDirectoryname) == false)
            {
                //LogInfo("Creating log directory :" + strDirectoryname);
                Directory.CreateDirectory(strDirectoryname);
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    public void Log(String message)
    {
        DateTime datet = DateTime.Now;
        if (sw == null)
        {
            sw = new StreamWriter(strPathName, true);
        }
        sw.Write(message);
        sw.Flush();
    }

    /// <summary>
    /// Close stream
    /// </summary>
    public void Close()
    {
        if (sw != null)
        {
            sw.Close();
            sw = null;
        }
    }

}

Thanks in advance

  • 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-30T15:50:31+00:00Added an answer on May 30, 2026 at 3:50 pm

    How about:

    public void Log(String message, params object[] args)
    {
        DateTime datet = DateTime.Now;
        if (sw == null)
        {
            sw = new StreamWriter(strPathName, true);
        }
        sw.Write(String.Format(message,args));
        sw.Flush();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that I'm converting from Perl to Java. It makes pretty
I have been using the mysql api in PHP, and am now converting to
I am in the process of converting some code from C# to Java. I
I've been tasked with converting some text log files from a test reporting tool
Ok, I have a strange exception thrown from my code that's been bothering me
I have just been through one of my apps converting the authentication from authlogic
I have a bit of code that I have been tasked with converting to
So I've been tasked with converting some T-SQL code to C code. Whoever wrote
I am converting some code from HSLSL and XNA to CG and OpenGL. The
I have been tasked with converting several php classes into java classes, which is

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.