c++和c#的数据交互

c#写读csv

c#输出csv

public void WriteCSV(List<STL.Line> pointFs)
        {
            string fullPath = "saveCSV.csv";
            System.IO.FileInfo fi = new System.IO.FileInfo(fullPath);
            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }
            System.IO.FileStream fs = new System.IO.FileStream(fullPath, System.IO.FileMode.Create,
                System.IO.FileAccess.Write);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
            string data = "";

            for (int i = 0; i < pointFs.Count(); i++)//写入列名
            {
                data += i.ToString() + ",";
                for (int j = 0; j < pointFs[i].Polygon.Count(); j++)
                {
                    data += pointFs[i].Polygon[j].X.ToString();
                    data += ",";
                    data += pointFs[i].Polygon[j].Y.ToString();
                    if (j != pointFs[i].Polygon.Count() - 1)
                    {
                        data += ",";
                    }
                }
                
                data += '\t';
                sw.WriteLine(data);
                data = "";

            }
            

            sw.Close();
            fs.Close();

        }

c#读取csv

public List<List<PointF>> ReadCSV(string fileName)
        {
            List<List<PointF>> Result;
            List<PointF> da;
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);

            //记录每次读取的一行记录
            string strLine = null;
            //记录每行记录中的各字段内容
            string[] arrayLine = null;
            //分隔符
            string[] separators = { "," };
            //判断,若是第一次,建立表头
            bool isFirst = false;

            //列的个数
            int dtColumns = 0;

            //逐行读取CSV文件
            Result = new List<List<PointF>>();
            while ((strLine = sr.ReadLine()) != null)
            {
                strLine = strLine.Trim();//去除头尾空格
                arrayLine = strLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);//分隔字符串,返回数组

                if (true)  //建立表头
                {
                    dtColumns = arrayLine.Length;//列的个数
                    da = new List<PointF>();
                    da.Clear();
                    for (int i = 0; i < dtColumns - 1; i = i + 2)
                    {
                        PointF p = new PointF(float.Parse(arrayLine[i]), float.Parse(arrayLine[i + 1]));
                        da.Add(p);

                    }
                    Result.Add(da);
                    isFirst = false;
                }
                else   //表内容
                {

                }

            }
            sr.Close();
            fs.Close();
            return Result;

        }

c++写读csv

c++输出csv

void WritecsvFile(SAveClass s,string name)
{
	ofstream file;
	string sname = "./" + name + ".csv";
	file.open(sname);
	if (!file.is_open(), ios::out)
	{
		cout << "文件打开失败" << endl;
	}
	ofstream file1;

	for (int i = 0; i < s.DATA.size(); i++)
	{

		int a = s.DATA[i].size();
		for (int j = 0; j < a; j++)
		{
			double x, y;
			x = s.DATA[i][j].x ;
			y = s.DATA[i][j].y ;
			file << x << "," << y;
			if (j == a - 1)//判断一行数据是否读完
			{
				file << "\n";
			}
			else
			{
				file << ",";

			}

		}


		//file1.write((char*)&a, sizeof(a));

	}



	file.close();

}

c++读取csv

QVector<QVector<QPointF>> ReadcsvFile()
{
	QVector<QVector<QPointF>> Rcsvdata;
	QVector<QPointF>  data1;
	QPointF p;
	
	//ifstream fin("saveCSV小人.csv"); //打开文件流操作saveCSV底座0
	fstream fin("saveCSV底座1.csv");
	string line;
	vector<string> fields; //声明一个字符串向量
	string field;
	
	while (getline(fin, field))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
	{ 
		fields.push_back(field);
		
	}
	for (auto it = fields.begin(); it != fields.end(); it++)
	{
		string str;

		//其作用是把字符串分解为单词(在此处就是把一行数据分为单个数据)
		istringstream istr(*it);

		//将字符串流 istr 中的字符读入到 str 字符串中,读取方式是以逗号为分隔符 
           // str 对应第六列数据   
		double x, y;
		getline(istr, str, ',');
		x = atof(str.c_str());//因为第一列有问题,所以把第一列先弄出来
		data1.clear();
		double zoom = 1;

		while (getline(istr, str, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
		{
			x = atof(str.c_str())*zoom;
			getline(istr, str, ',');
			y = atof(str.c_str()) * zoom;
			p = QPointF(x, y);
			data1.push_back(p);
		}
		if (data1.size() > 0)
		{
			Rcsvdata.push_back(data1);
		}
		
	}

	return Rcsvdata;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值