我可以用什么而不是strstream :: freeze?

时间:2017-07-13 06:34:44

标签: c++

我正在处理一些旧的c ++代码,它有strstream类,不推荐使用。我需要用工作替换它们。

在我的标题文件中,我得到了这个:

 public class APPInfo
 {
        public String Machine { get; set; }
        public String IP { get; set; }
        public String Port { get; set; }
        public String App { get; set; }
        public ClientApplicationState Status { get; set; }
        public String Message { get; set; }
        public String TimeStamp { get; set; }
        public String CPUUse { get; set; }
        public String MemoryUse { get; set; }
        public String TotalCPUUse { get; set; }
        public String TotalMemoryUse { get; set; }
 }

public static ConcurrentQueue<APPInfo> AppQ = new ConcurrentQueue<APPInfo>();

 public static void Connect(Int16 Time)
 {    
       AppQ.Enqueue(new APPInfo { Machine = "One", IP = "127.0.0.1", Port = "23000", App = "TestServer1", Status = ClientApplicationState.OK, TimeStamp = DateTime.Now.ToString(), CPUUse = "80.0", MemoryUse = "81.0", TotalCPUUse = "86.0", TotalMemoryUse = "87.0" });

       AppQ.Enqueue(new APPInfo { Machine = "One", IP = "127.0.0.1", Port = "23001", App = "TestServer2", Status = ClientApplicationState.ERROR, TimeStamp = DateTime.Now.ToString(), CPUUse = "82.0", MemoryUse = "83.0", TotalCPUUse = "88.0", TotalMemoryUse = "89.0" });

       AppQ.Enqueue(new APPInfo { Machine = "Two", IP = "127.0.0.2", Port = "23002", App = "TestServer3", Status = ClientApplicationState.WARNING, TimeStamp = DateTime.Now.ToString(), CPUUse = "84.0", MemoryUse = "85.0", TotalCPUUse = "90.0", TotalMemoryUse = "91.0" });        
 }

 private void CheckQueue()
 {
     AppIP.Connect(50);
     APPInfo first = new APPInfo();         

     if (AppIP.AppQ.Count > 0)
     {
         AppIP.AppQ.TryDequeue(out first);

        MessageBox.Show(first.App, first.IP, first.Port, first.Status.ToString(), first.Message, first.TimeStamp, first.CPUUse, first.MemoryUse, first.Machine, first.TotalCPUUse, first.TotalMemoryUse + "\n");
     }
     else
     {
          MessageBox.Show("There is nothing in the queue to process.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
     }
 }

和.c文件使用标题:

ostrstream          tokenBuff;

我用 ostringstream 替换了 ostrstream 。 ostringstream有seekp()但不是freeze()。我该如何处理这个问题。感谢。

2 个答案:

答案 0 :(得分:6)

freeze()的情况下,您不需要std::ostringstream - 调用此函数的必要性实际上是strstream首先被弃用的原因之一。由于其设计(从char*返回str()),不清楚谁应该清除strstream持有的缓冲区,并且必须使用freeze()来表示您想要的信号每次调用strstreamstr()本身都会这样做 - 如果std::ostringstream您不需要担心这一点,str()会返回{{1}的副本}}

答案 1 :(得分:1)

freeze中的ostringsteam无法替代,因为不再需要它。 ostrstream freeze()用于解决str()返回c样式(char *)字符串的不足。