publicvoidWriteCSV(List<STL.Line> pointFs){string fullPath ="saveCSV.csv";System.IO.FileInfo fi =newSystem.IO.FileInfo(fullPath);if(!fi.Directory.Exists){
fi.Directory.Create();}System.IO.FileStream fs =newSystem.IO.FileStream(fullPath, System.IO.FileMode.Create,
System.IO.FileAccess.Write);System.IO.StreamWriter sw =newSystem.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
publicList<List<PointF>>ReadCSV(string fileName){List<List<PointF>> Result;List<PointF> da;FileStream fs =newFileStream(fileName, FileMode.Open, FileAccess.Read);StreamReader sr =newStreamReader(fs, Encoding.UTF8);//记录每次读取的一行记录string strLine =null;//记录每行记录中的各字段内容string[] arrayLine =null;//分隔符string[] separators ={","};//判断,若是第一次,建立表头bool isFirst =false;//列的个数int dtColumns =0;//逐行读取CSV文件
Result =newList<List<PointF>>();while((strLine = sr.ReadLine())!=null){
strLine = strLine.Trim();//去除头尾空格
arrayLine = strLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);//分隔字符串,返回数组if(true)//建立表头{
dtColumns = arrayLine.Length;//列的个数
da =newList<PointF>();
da.Clear();for(int i =0; i < dtColumns -1; i = i +2){PointF p =newPointF(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
voidWritecsvFile(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;}