using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ClearRepeatItems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileName=string.Empty;
private void openFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
UpdateItems(fileName);
}
}
StringBuilder sb = new StringBuilder();
List<string> listNames = new List<string>();
string[] listNamesArray;
private void UpdateItems(string fileName)
{
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (!listNames.Contains(line))
{
listNames.Add(line);
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
listNamesArray = listNames.ToArray();
listBox1.Items.AddRange(listNamesArray);
}
private void saveFile_Click(object sender, EventArgs e)
{
string newFileName = Path.GetFileNameWithoutExtension(fileName) + "_New." + Path.GetExtension(fileName);
FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter(newFileName, false, Encoding.Default);
for (int i = 0; i < listNamesArray.Length; i++)
{
sb.AppendLine(listNamesArray[i]);
}
sw.Write(sb.ToString());
sw.Close();
}
private void saveXml_Click(object sender, EventArgs e)
{
StringBuilder sbXml = new StringBuilder();
sbXml.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sbXml.AppendLine("<Contacts>");
for (int i = 0; i < listNamesArray.Length; i++)
{
string name = listNamesArray[i].Split(',')[0];
string tel = listNamesArray[i].Split(',')[1];
sbXml.AppendLine("<Contact>");
sbXml.AppendLine("<Name>" + name + "</Name>");
sbXml.AppendLine("<Starred>0</Starred>");
sbXml.AppendLine("<PhoneList>");
sbXml.AppendLine("<Phone Type=\"2\">" + tel + "</Phone>");
sbXml.AppendLine("</PhoneList>");
sbXml.AppendLine("<Account value=\"0\">");
sbXml.AppendLine("<Name>Phone</Name>");
sbXml.AppendLine("<Type>com.android.huawei.phone</Type>");
sbXml.AppendLine("</Account>");
sbXml.AppendLine("<GroupList>");
sbXml.AppendLine("<GroupName>家庭</GroupName>");
sbXml.AppendLine("</GroupList>");
sbXml.AppendLine("</Contact>");
}
sbXml.AppendLine("</Contacts>");
string newFileName = Path.GetFileNameWithoutExtension(fileName) + "_New.xml";
FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter(newFileName, false, Encoding.UTF8);
&nb
#define MAXSIZE 100 //定义一个常量 //顺序表结构定义 typedef struct { int data[MAXSIZE]; //存放数据 int length; //存储顺序表长度 }Sqlist; //考试可以简写如下: // int A[MAXSIZE]; // int length; //单链表结构定义 typedef struct LNode { int data; struct LNode *next; }LNode; //双链表结构定义 typedef struct DLNode { int data; struct DLNode *prior; struct DLNode *next; }DLNode;
现在用newLISP编写TCP客户端来测试程序:
chenshu@chenshu-beijing:~$ newlisp newLISP v.10.4.5 64-bit on Linux IPv4/6 UTF-8 libffi, execute 'newlisp -h' for more info. > (set 'socket (net-connect "localhost" 8888)) 3 > (net-send socket "a")
上面的代码略做解释:
1.(net-connect ...) 用来连接本地8888端口,返回的socket文件被赋值给了socket变量
2.(net-send可以对socket代表的连接发送字符串"a"
此时服务程序打印如下;
chenshu@chenshu-beijing:~/NetBeansProjects/CppApplication_4/dist/Debug/GNU-Linux-x86$ ./cppapplication_4 count1:1 count2:2 count3:2 The new connection object is starting now. ~Connection Operation canceled count3:2
注意到了么,Connection的析勾函数被调用后,居然它的成员函数AfterReadChar还会被调用。错在哪里?
因为之前
async_read(socket, buffer(read_buffer_), boost::bind(&Connection::AfterReadChar, this, _1));绑定了this指针。所以即便shared_ptr的引用计数为0导致析勾函数被调用,而this指针由于被bind_t保存,所以asio框架仍然能够调用到这个被删除的对象的成员函数。
这种行为是可怕的,因为下次可能就是程序崩溃。关键问题是应该还是将shared_ptr拿去绑定,这样只要一切都在shared_ptr的管理内,asio就不会错误的调用已经被销毁的对象。
所以下一篇就要解决这个问题。