!VC中用C++编程按行读取txt文档中的数据并显示
求助!!!VC中用C++编程按行读取txt文档中的数据并显示
在VC中用C++编写程序,按行读取txt文本中的数据,并显示到屏幕上。另外最好能够做到根据文件中的“,”和“;”分号,在需要的时候随意对现实的数据分段。至少能够把数据读出来。
现在勉强能够做到按行读数据,但是无法读取下一行及全部数据。希望能有高手帮助把程序写出来。
------解决方案--------------------
mfc对话框 对话框类添加指针成员
class CMfcdlg2014Dlg : public CDialog
{
// Construction
public:
CStdioFile *p;
CMfcdlg2014Dlg(CWnd* pParent = NULL); // standard constructor
........
对话框类构造函数new 出来
.....
p = new CStdioFile(_T("测试文本.txt"), CFile::modeRead);
......
添加关闭消息响应函数,关闭文件 delete释放对象
void CMfcdlg2014Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
p->Close();
delete p;
CDialog::OnClose();
}
按钮响应函数
void CMfcdlg2014Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString s;
if(!feof(p->m_pStream))
{
p->ReadString(s);
SetDlgItemText(IDC_EDIT1, s);
CString s1,s2,s3;
int n1, n2;
n1 = s.Find(',',0);
s1 = s.Left(n1);
n2 = s.Find(',', n1+1);
s2 = s.Mid(n1+1, n2-n1-1);
s3 = s.Mid(n2+1);
SetDlgItemText(IDC_EDIT2, s1);
SetDlgItemText(IDC_EDIT3, s2);
SetDlgItemText(IDC_EDIT4, s3);
}
else
{
MessageBox(_T("已经读完"));
}
}
效果图
------解决方案--------------------
------解决方案--------------------
<iostream.h>和<iostream>的区别就是一个新一个旧
都是头文件只不过新版用名字控件规范了一下
使用这些东西没有一定之规
怎么好用怎么来就行
怎么改能够实现你的功能你就怎么改
------解决方案--------------------
比如:CString 要包含#include"Afx.h"
你上MSDN查看
------解决方案--------------------
在VC中用C++编写程序,按行读取txt文本中的数据,并显示到屏幕上。另外最好能够做到根据文件中的“,”和“;”分号,在需要的时候随意对现实的数据分段。至少能够把数据读出来。
现在勉强能够做到按行读数据,但是无法读取下一行及全部数据。希望能有高手帮助把程序写出来。
------解决方案--------------------
mfc对话框 对话框类添加指针成员
class CMfcdlg2014Dlg : public CDialog
{
// Construction
public:
CStdioFile *p;
CMfcdlg2014Dlg(CWnd* pParent = NULL); // standard constructor
........
对话框类构造函数new 出来
.....
p = new CStdioFile(_T("测试文本.txt"), CFile::modeRead);
......
添加关闭消息响应函数,关闭文件 delete释放对象
void CMfcdlg2014Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
p->Close();
delete p;
CDialog::OnClose();
}
按钮响应函数
void CMfcdlg2014Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString s;
if(!feof(p->m_pStream))
{
p->ReadString(s);
SetDlgItemText(IDC_EDIT1, s);
CString s1,s2,s3;
int n1, n2;
n1 = s.Find(',',0);
s1 = s.Left(n1);
n2 = s.Find(',', n1+1);
s2 = s.Mid(n1+1, n2-n1-1);
s3 = s.Mid(n2+1);
SetDlgItemText(IDC_EDIT2, s1);
SetDlgItemText(IDC_EDIT3, s2);
SetDlgItemText(IDC_EDIT4, s3);
}
else
{
MessageBox(_T("已经读完"));
}
}
效果图
------解决方案--------------------
#include<stdio.h>
#include<cstdlib>
int main(){
FILE** fp=new (FILE*);
char c;
if (fopen_s(fp,"D:\\portOpen.txt", "r") != NULL){
printf("error...\n");
return -1;
}
while (fscanf_s(*fp, "%c", &c, 1) == 1){
if (c == ','
------解决方案--------------------
c == ';'){
putchar(c);
printf("\n");
}
else
putchar(c);
}
system("pause");
}
------解决方案--------------------
<iostream.h>和<iostream>的区别就是一个新一个旧
都是头文件只不过新版用名字控件规范了一下
使用这些东西没有一定之规
怎么好用怎么来就行
怎么改能够实现你的功能你就怎么改
------解决方案--------------------
CStdioFile file;
file.Open((LPCTSTR)_T("StudentGrade.txt"), CFile::modeReadWrite);
CString str;
while (file.ReadString(str))
{
}
用file.ReadString 读取一行,然后会指向下一行。
你这是MFC类库里的吧,能不能在源文件中实现
控制台中也可以用很多MFC类库里的东西,有些不能用,如:和图形相关的库
如果要使用这些函数,需要包括哪些头文件
比如:CString 要包含#include"Afx.h"
你上MSDN查看
------解决方案--------------------
CStdioFile file;
file.Open((LPCTSTR)_T("StudentGrade.txt"), CFile::modeReadWrite);
CString str;
while (file.ReadString(str))
{
}
用file.ReadString 读取一行,然后会指向下一行。
你这是MFC类库里的吧,能不能在源文件中实现
控制台中也可以用很多MFC类库里的东西,有些不能用,如:和图形相关的库
如果要使用这些函数,需要包括哪些头文件
比如:CString 要包含#include"Afx.h"