脑电 关于CSV 文件的读取

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>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值