怎样读取这样一个文本文件?

AMDCPU 2005-07-30 07:50:13
怎样读取类似这样一个文本文件?
格式如下:
001/12.1
002/24
003/42.5
004/45.6
008/75.3
015/456
120/62.3


总之就是 序号/数值(浮点类型)

怎样读入一个 序号数组char Numb[][4]和 数值fValue[] 数组中?
用CFile类打开操作。
...全文
158 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
candcjiajia 2005-07-31
  • 打赏
  • 举报
回复
用c++中的 fstream 以及string流也能实现你想要的结果,而不必用CFile类。这点你可以参考一下有关c++的书籍,挺简单的。
ggw 2005-07-31
  • 打赏
  • 举报
回复
按行读后,转成C字串,用sscanf加格式码[^/]应该可以。
中级伴读 2005-07-31
  • 打赏
  • 举报
回复
CStdioFile->ReadString()
38062708 2005-07-31
  • 打赏
  • 举报
回复
char szenter[3] = "\r\n";
char sztab[2] = "\t";
char buftemp[512];
CString str,strtemp;
int index = 0;

DWORD postemp = 0;
DWORD posf = 0;
DWORD posa = 0;
DWORD pos = 0;
DWORD loffset = 0;
pos = m_file.SeekToEnd();
m_file.SeekToBegin();
while (m_file.GetPosition() < pos)
{
memset(buftemp,0,512);
m_file.Read(buftemp,512);
if (buftemp[0] == 0x2a)// "*" is EOF
break;
str.Format("%s",buftemp);
postemp = str.Find(szenter,0);
if (postemp == -1)
{
AfxMessageBox("Please check the File !");
return FALSE;
}
postemp += 2;
loffset += postemp;
if (loffset > pos)
break;
m_file.Seek(loffset,CFile::begin);

if (index == 0)
{
index ++;
continue;
}

memset(&ma,0,sizeof(AMMETERADDR));
posa = str.Find(sztab,0);
strtemp = str.Left(posa);
sprintf(ma.szmeterno,"%s",strtemp);
posa++;
postemp = str.Find(sztab,posa);
strtemp = str.Mid(posa,postemp - posa);
postemp += 1;
posa = postemp;
postemp = str.Find(sztab,posa);
strtemp = str.Mid(posa,postemp - posa);
sprintf(ma.szzoneno,"%s",strtemp);
postemp += 1;
posa = postemp;
postemp = str.Find(sztab,posa);
strtemp = str.Mid(posa,postemp - posa);
sprintf(ma.szdcuno,"%s",strtemp);
postemp += 1;
posa = postemp;
postemp = str.Find(sztab,posa);
strtemp = str.Mid(posa,postemp - posa);
ma.nmeterindex = atoi(strtemp);

m_arrMeterAddr.Add(ma);
}

参考一下吧
赖勇浩 2005-07-30
  • 打赏
  • 举报
回复
使用CStdioFile 的成员函数 ReadString 来读去每一行,然后CString的Find等查找“/”,然后截取出两部分字符串,再将后面的字符串用atof函数转成float。
nonocast 2005-07-30
  • 打赏
  • 举报
回复
先readline
然后sscanf一下就ok了
AntonlioX 2005-07-30
  • 打赏
  • 举报
回复
你看看msdn啊 里面有代码啊
叶子哟 2005-07-30
  • 打赏
  • 举报
回复
查资料哈,点搜索
CStdioFile InFile;
CString InString;
if (InFile.Open(filename,CFile::modeRead|CFile::modeWrite))
{
InFile.ReadString(firstline);
...
InFile.Close();
}
AMDCPU 2005-07-30
  • 打赏
  • 举报
回复
给出点代码啊,怎么读?
AntonlioX 2005-07-30
  • 打赏
  • 举报
回复
还可以使用

FILE 来实现

调用 fgets 函数 可以读每一行
AntonlioX 2005-07-30
  • 打赏
  • 举报
回复
使用CStdioFile 的成员函数 ReadString 来读去每一行
AMDCPU 2005-07-30
  • 打赏
  • 举报
回复
问题是怎么读整行啊?
oyljerry 2005-07-30
  • 打赏
  • 举报
回复
整行读入,然后CString的Find等查找“/”,然后截取出两部分字符串

16,472

社区成员

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

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

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