如何一次读取两行txt文件 连续读两行阿比如readstring两次 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Google 搜索 CStdioFile 类的用法。 CString CMyDataBaseView::split(CString str,int index){ if(index==1) { int p = str.Find(","); return str.Mid(0,p); } else if(index==2) { int p = str.Find(","); int q = str.Find(",",p+1); if(q==-1) return str.Mid(p+1,str.GetLength()-p); else return str.Mid(p+1,q-p-1); } else { CString stemp(""); return stemp; }}void CMyDataBaseView::OnButtonTrackmapShow() { // TODO: Add your control notification handler code here char buf[100]; //临时保存读取出来的文件内容 CString str; CClientDC dc(this); CPen pen(PS_SOLID,1,RGB(255,0,0)); dc.SelectObject(&pen); CWnd* pWnd = GetDlgItem(IDC_EDIT_FILEPATH); pWnd->GetWindowText(m_strFilePathName); UpdateData(TRUE); // CFileException e; if( !f.Open( m_strFilePathName, CFile::modeCreate | CFile::modeWrite, &e ) ) { //#ifdef _DEBUG //afxDump << "File could not be opened " << e.m_cause << "\n"; //#endif MessageBox("文件没有打开"); } else { while(f.ReadString( buf, 99 )) // f.ReadString( buf, 99 ); { str=buf; CPoint a; a.x=atoi(split(str,1)); a.y=atoi(split(str,2)); dc.MoveTo(a); char buff[100]; f.ReadString( buff, 99 ); str=buff; CPoint b; b.x=atoi(split(str,1)); b.y=atoi(split(str,2)); dc.LineTo(b); } }}以上是我的程序代码,我的txt文件格式如下:12,3,0,0,034,534,1,0,0444,5,0,0,0....其中前两列代表x,y坐标,我要实现的功能就是以(12,3)(34,534)(444,5)等等为坐标顺次连接成线段显示出来。现在编译通过,但是运行时显示出现不明错误。拜托各位多多帮忙!!! while(f.ReadString( buf, 99 )) // f.ReadString( buf, 99 ); { str=buf; CPoint a; a.x=atoi(split(str,1)); a.y=atoi(split(str,2)); dc.MoveTo(a); char buff[100]; f.ReadString( buff, 99 ); str=buff; CPoint b; b.x=atoi(split(str,1)); b.y=atoi(split(str,2)); dc.LineTo(b); } 我就想问下这段代码,我编的有问题没。主要是我对ReadString函数不熟悉。 我这样编实现的是读取两行吗?while读取的是一行,然后在循环体内我读取的是下一行。一个循环下来,再执行的是第三行还是第二行?不知道楼上的能否听懂我的意思。我是新手,每次编译我都是整体编译,所以也不知道究竟具体是哪句有问题了。唉。 直接readstring好了,不用加上99恩再循环一次,将读出的是第三行 为啥要用readstring啊,多麻烦,读取出来还要转换直接用sprintf不就得了?或者fprintf这样的函数,用字符串格式一格式化,读出来的就是你要的类型的 关于资源泄漏的问题(大家帮忙看看)GDI对象9999 理论上,一台单ip的机器最多能建立多少个网络连接?? 一个简单而又想不到的问题 求头文件dshow.h? 郁闷...散分...顺便问一下 高分征求ppoe拨号程序源代码? 小白求助,如何用VC读取CAD的dwg文件中的数据 一个很简单的显示数据的问题,我很迷惑,帮帮我 关于ICON的一个问题 简单问题 据说是国内最权威的职业测评,你也来测试一下吧 如何定位到txt文本的倒数第二行?
Google 搜索 CStdioFile 类的用法。
{
if(index==1)
{
int p = str.Find(",");
return str.Mid(0,p);
}
else if(index==2)
{
int p = str.Find(",");
int q = str.Find(",",p+1);
if(q==-1)
return str.Mid(p+1,str.GetLength()-p);
else
return str.Mid(p+1,q-p-1);
}
else
{
CString stemp("");
return stemp;
}
}
void CMyDataBaseView::OnButtonTrackmapShow()
{
// TODO: Add your control notification handler code here
char buf[100]; //临时保存读取出来的文件内容
CString str; CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
dc.SelectObject(&pen);
CWnd* pWnd = GetDlgItem(IDC_EDIT_FILEPATH);
pWnd->GetWindowText(m_strFilePathName);
UpdateData(TRUE);
// CFileException e;
if( !f.Open( m_strFilePathName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
//#ifdef _DEBUG
//afxDump << "File could not be opened " << e.m_cause << "\n";
//#endif
MessageBox("文件没有打开");
} else
{
while(f.ReadString( buf, 99 ))
// f.ReadString( buf, 99 );
{
str=buf;
CPoint a;
a.x=atoi(split(str,1));
a.y=atoi(split(str,2));
dc.MoveTo(a); char buff[100]; f.ReadString( buff, 99 );
str=buff;
CPoint b;
b.x=atoi(split(str,1));
b.y=atoi(split(str,2));
dc.LineTo(b);
}
}
}以上是我的程序代码,我的txt文件格式如下:
12,3,0,0,0
34,534,1,0,0
444,5,0,0,0
....
其中前两列代表x,y坐标,我要实现的功能就是以(12,3)(34,534)(444,5)等等为坐标顺次连接成线段显示出来。
现在编译通过,但是运行时显示出现不明错误。
拜托各位多多帮忙!!!
// f.ReadString( buf, 99 );
{
str=buf;
CPoint a;
a.x=atoi(split(str,1));
a.y=atoi(split(str,2));
dc.MoveTo(a); char buff[100]; f.ReadString( buff, 99 );
str=buff;
CPoint b;
b.x=atoi(split(str,1));
b.y=atoi(split(str,2));
dc.LineTo(b);
}
我就想问下这段代码,我编的有问题没。
主要是我对ReadString函数不熟悉。
while读取的是一行,然后在循环体内我读取的是下一行。一个循环下来,再执行的是第三行还是第二行?
不知道楼上的能否听懂我的意思。
我是新手,每次编译我都是整体编译,所以也不知道究竟具体是哪句有问题了。
唉。
恩
再循环一次,将读出的是第三行
直接用sprintf不就得了?或者fprintf这样的函数,用字符串格式一格式化,读出来的就是你要的类型的