如何在MFC中读取txt文件中的数据?

wyl1267 2010-06-07 09:57:26
很久未用VC编程了,想问个基本问题,有个test.txt文件格式如下(行数不定):

Freq Val
--------------
50.0 -17.8
52.9 -17.6
56.0 -17.4
59.4 -16.9
63.0 -16.2

需将Freq下的数据存入freq[]数组中,将Val下的数据存入val[]数组中.
该用CStdioFile还是CFile较合适?
请写一段示范程序,谢谢!
...全文
24843 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiger12551 2012-10-18
  • 打赏
  • 举报
回复
终于学会了C和C++对文件读操作,谢谢
wyl1267 2010-06-09
  • 打赏
  • 举报
回复
谢谢楼上提供了简洁的代码,我又学到两招,对于这个基础问题,还是基本的C函数管用。
wltg2001 2010-06-08
  • 打赏
  • 举报
回复
用CStdioFile吧,一行行的读,比较方便,读到CString里后,用CString::Find去找空格或是-都很方便
mngzilin 2010-06-08
  • 打赏
  • 举报
回复
CFile
pcgg5244 2010-06-08
  • 打赏
  • 举报
回复
打错了
float freq[100],val[100];
FILE *fp;
fp=fopen("test.txt","r+");
int i=0;
while(!feof(fp))
{
fscanf(fp,"%f -%f\r\n",freq+i,val+i);
i++;
}
fclose(fp);
pcgg5244 2010-06-08
  • 打赏
  • 举报
回复
不过我觉得用FILE更方便来着
float freq[100],val[100];
FILE *fp;
fp=fopen("test.txt","r+");
int i=0;
while(!feof(fp))
{
fscanf(fp,"%f -%f\r\n",a+i,b+i);
}
fclose(fp);
pcgg5244 2010-06-08
  • 打赏
  • 举报
回复
CStdioFile file;
file.Open("test.txt",CFile::modeRead);
CString strLine;
int i=0;
float freq[100];
float val[100];
for(int j=0;j<2;j++)
file.ReadString(strLine);//MS这里得跳两行
while(file.ReadString(strLine))
{
sscanf(strLine,"%f -%f",freq+i,val+i);
i++;
}
file.Close();
owentjr 2010-06-08
  • 打赏
  • 举报
回复
学习到了
Eleven 2010-06-08
  • 打赏
  • 举报
回复

#define MAX_COUNT 256

try
{
CStdioFile file;
file.Open(_T("F:\\11.txt"), CFile::modeRead);
CString strLine(_T(""));
CString strText(_T(""));
LPCTSTR szToken(_T(" "));
int curPos = 0;
int nIndex = 0;
double freq[MAX_COUNT] = {0};
double val[MAX_COUNT] = {0};
int nCount = 0;

while(file.ReadString(strLine))
{
curPos = 0;
strLine.Trim(_T(" "));
nIndex = 0;

if(nCount >= MAX_COUNT)
{
break;
}

while(_T("") != (strText = strLine.Tokenize(szToken, curPos)))
{
if(++nIndex%2)
{
freq[nCount] = _tcstod(strText, NULL);
}
else
{
val[nCount] = _tcstod(strText, NULL);
}
}
nCount++;
}
file.Close();

CString strTmp(_T(""));
CString strMsg(_T(""));
for(int i=0; i<nCount; i++)
{
strTmp.Format(_T("freq=%.1lf, val=%.1lf\r\n"), freq[i], val[i]);
strMsg += strTmp;
}
AfxMessageBox(strMsg);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
wangli820 2010-06-08
  • 打赏
  • 举报
回复
CStdioFile myFile, File;
if(myFile.Open(CBApp::getCurrModulePath()+_T("filename.txt"), CFile::modeRead))
{
while (myFile.ReadString(str))
{
//处理读到的每一行数据
}
myFile.Close();
}
社会栋梁 2010-06-07
  • 打赏
  • 举报
回复
CStdioFile功能正合适
CStdioFile Class Members
Data Members

m_pStream Contains a pointer to an open file.


Construction

CStdioFile Constructs a CStdioFile object from a path or file pointer.


Text Read/Write

ReadString Reads a single line of text.
WriteString Writes a single line of text.
oyljerry 2010-06-07
  • 打赏
  • 举报
回复
CStdioFile刚好可以按行读取,ReadString到CString中,然后根据空格来分隔两个数据,分别放入对应的数组
zyrr159487 2010-06-07
  • 打赏
  • 举报
回复
MFC主要读写文件的类是CFile,而CStdioFile类是派生自CFile类的,主要增加了一个按行的方式读取/写入文件每行字符串的功能
读写TXT文件,写入和输出必须统一格式
CStdioFile myFile, File;
if(myFile.Open(CBApp::getCurrModulePath()+_T("filename.txt"), CFile::modeRead))
{
//读取
myFile.ReadString(str);
myFile.Close();
}
else
{
//创建
File.Open(CBApp::getCurrModulePath()+_T("filename.txt"), CFile::modeWrite | CFile::modeCreate);
File.WriteString(sFileName);
File.Close();
}
//写入
File.Open(CBApp::getCurrModulePath()+_T("filename.txt"), CFile::modeWrite );
File.WriteString(sFileName);
File.Close();

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧