[英]C# equivalent of fprintf
I have been converting some code from C++ to C#. 我一直在将一些代码从C ++转换为C#。 My lack of understanding of C# API doesnt let me find the equivalent of fprintf.
我对C#API缺乏了解并不能让我找到相当于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.
“Log”方法目前仅记录字符串。 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.
在C ++中,我有fprintf进行转换。 How can I achieve something similar in C# ??
我怎样才能在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 提前致谢
You can create a StreamWriter to wrap your FileStream
, and then use Write
to get something like 您可以创建一个StreamWriter来包装您的
FileStream
,然后使用Write
来获得类似的东西
StreamWriter writer = new StreamWriter(fs);
writer.Write("Wheel1: {0} \t Wheel2: {1} \t Dist: {2}", Wheel0, Wheel1, TotalDist);
It sounds like you're looking for String.Format
. 听起来你正在寻找
String.Format
。
The Write()
and WriteLine()
methods actually do the same thing. Write()
和WriteLine()
方法实际上做同样的事情。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.