脑电 关于CSV 文件的读取

来源:互联网 发布:淘宝叶彬儿彩妆 编辑:程序博客网 时间:2024/04/30 06:48

csv是指“逗号分割值”文件(comma separated value),就是保存以逗号分隔的数值的纯文本文件,Excel可以直接打开。
csv文件通常有多行,每行保存一组数据,用来记录实验数据等最合适不过了,csv文件内容示例:
10:23,0.123,0.234
10:24,0,456,0.789
10:26,1.224,1.456
VC中也常常csv文件来存取小规模的数据,下面演示了如何保存和读取csv文件。

<pre code_snippet_id="272648" snippet_file_name="blog_20140403_1_7046893" name="code" class="cpp">///////////写csv文件////////////  void CTestDlg::OnBnClickedButton1()  {  <span style="white-space:pre">  </span>CStdioFile file;  <span style="white-space:pre">  </span>file.Open(_T("./aaa.csv"),CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);  <span style="white-space:pre">  </span>file.SeekToEnd();  <span style="white-space:pre">  </span>CString str;  <span style="white-space:pre">  </span>GetDlgItemText(IDC_EDIT1,str);  <span style="white-space:pre">  </span>double d1 = _wtof((LPCTSTR)str);  <span style="white-space:pre">  </span>GetDlgItemText(IDC_EDIT2,str);  <span style="white-space:pre">  </span>double d2 = _wtof((LPCTSTR)str);  <span style="white-space:pre">  </span>GetDlgItemText(IDC_EDIT3,str);  <span style="white-space:pre">  </span>double d3 = _wtof((LPCTSTR)str);  <span style="white-space:pre">  </span>GetDlgItemText(IDC_EDIT4,str);  <span style="white-space:pre">  </span>double d4 = _wtof((LPCTSTR)str);  <span style="white-space:pre">  </span>COleDateTime t = COleDateTime::GetCurrentTime();  <span style="white-space:pre">  </span>str.Format(  <span style="white-space:pre">      </span>_T("%d.%d.%d ")  <span style="white-space:pre">      </span>_T(", %d:%d:%d ")  <span style="white-space:pre">      </span>_T(", %f , %f , %f , %f")  <span style="white-space:pre">      </span>_T("\n")  <span style="white-space:pre">      </span>,t.GetYear(),t.GetMonth(),t.GetDay()  <span style="white-space:pre">      </span>,t.GetHour(),t.GetMinute(),t.GetSecond()  <span style="white-space:pre">      </span>,d1,d2,d3,d4);  <span style="white-space:pre">      </span>file.WriteString(str);  <span style="white-space:pre">      </span>file.Close();  }  
//////////读csv文件///////////  void CTestDlg::OnBnClickedButton4()  {  <span style="white-space:pre">  </span>CStdioFile file;  <span style="white-space:pre">  </span>file.Open(_T("./aaa.csv"),CFile::modeRead);  <span style="white-space:pre">  </span>CString str;  <span style="white-space:pre">  </span>while(file.ReadString(str))  <span style="white-space:pre">  </span>{  <span style="white-space:pre">      </span>str = str.Trim(_T(" "));  <span style="white-space:pre">      </span>CString substr[10];  <span style="white-space:pre">      </span>int count=0;  <span style="white-space:pre">      </span>int index = str.Find(_T(","));  <span style="white-space:pre">      </span>while (index != -1 && count<9)  <span style="white-space:pre">      </span>{  <span style="white-space:pre">          </span>substr[count++] = str.Left(index);  <span style="white-space:pre">          </span>str=str.Right(str.GetLength()-index-1);  <span style="white-space:pre">          </span>index = str.Find(_T(","));  <span style="white-space:pre">      </span>}  <span style="white-space:pre">      </span>substr[count++]=str;  <span style="white-space:pre">      </span>CString stmp;  <span style="white-space:pre">      </span>stmp.Format(_T("%s %s %s %s %s %s %s %s %s %s")  <span style="white-space:pre">              </span>,substr[0],substr[1],substr[2],substr[3],substr[4]  <span style="white-space:pre">              </span>,substr[5],substr[6],substr[7],substr[8],substr[9]  <span style="white-space:pre">              </span>);  <span style="white-space:pre">      </span>MessageBox(stmp);  <span style="white-space:pre">  </span>}  <span style="white-space:pre">  </span>file.Close();  }</pre><br>  <p></p>  <pre></pre>  <p></p>  
0 0