求教:TXT文档如何读取(VC,VS2008环境里的解决方案均可)

wangzf1983 2010-06-17 09:41:47
目的:读取E:\VCpp\IQattn.txt里的所有频率项的功率值,
//频率 功率值
2412 12.248
2417 12.319
2422 12.332
2427 12.324
2432 12.292
2437 12.243
2442 12.191
2447 12.118
2452 12.152
2457 12.18
2462 12.218
2467 12.284
2472 12.352
2484 12.426
然后写到同目录下的CableLoss.ini文档
目标格式是:
[pathloss]
2412=12.248
2417=12.319
2422=12.332
2427=12.324
2432=12.292
2437=12.243
2442=12.191
2447=12.118
2452=12.152
2457=12.18
2462=12.218
2467=12.284
2472=12.352
2484=12.426
在VC里面写入目标文档CableLoss.ini,小弟知道用WritePrivateProfileString命令解决。
WritePrivateProfileString("pathloss","2412",Value2412,"E:\\VCpp\\CableLoss.ini");
.
.
.
WritePrivateProfileString("pathloss","2484",Value2484,"E:\\VCpp\\CableLoss.ini");
请大侠指点...小弟在线恭候...
...全文
657 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 wangzf1983 的回复:]
感谢10楼帮忙!
一切搞定.
只是末尾行顶行多一个=号
不影响.
[/Quote]
怎么会多出一个等号来呢?你Debug下看看,是否还有什么特殊情况没有考虑到。。。
wangzf1983 2010-06-18
  • 打赏
  • 举报
回复
感谢10楼帮忙!
一切搞定.
只是末尾行顶行多一个=号
不影响.
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
感谢楼上的高人的大力无私奉献于支援,
代码很强,可惜小部分我不懂,榜样的力量,
催使我要努力学习呀,CString strValue(_T(""));
到吃饭时间了,
先吃个饭先,
回头在自己跑跑试试。
Great Thanks!
Eleven 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 visualeleven 的回复:]
可以结贴了

C/C++ code

try
{
CString strLine(_T(""));
CString strText(_T(""));

map<CString, CString> strMap;
int curPos = 0;
LPCTSTR szToken……
[/Quote]
记得加上
#include <map>
using namespace std;
洗洗睡去 2010-06-17
  • 打赏
  • 举报
回复
CStdioFile File;
File.Open(_T("D:\\test.txt"),CFile::modeReadWrite);
CString str,strtemp;
while(File.ReadString(strtemp))
{
str += strtemp;
}
str就是内容了
Eleven 2010-06-17
  • 打赏
  • 举报
回复
11.txt文本文件内容:
----------------------
2412 12.248
2417 12.319
2422 12.332
2427 12.324
2432 12.292
2437 12.243
2442 12.191
2447 12.118
2452 12.152
2457 12.18
2462 12.218
2467 12.284
2472 12.352
2484 12.426

11.ini文件内容:
-------------
[pathloss]
2412=12.248
2417=12.319
2422=12.332
2427=12.324
2432=12.292
2437=12.243
2442=12.191
2447=12.118
2452=12.152
2457=12.18
2462=12.218
2467=12.284
2472=12.352
2484=12.426
Eleven 2010-06-17
  • 打赏
  • 举报
回复
可以结贴了

try
{
CString strLine(_T(""));
CString strText(_T(""));

map<CString, CString> strMap;
int curPos = 0;
LPCTSTR szToken = _T(" ");
int nIndex = 0;

CString strName(_T(""));
CString strValue(_T(""));

CStdioFile file;
file.Open(_T("F:\\11.txt"), CFile::modeRead);
while(file.ReadString(strLine))
{
curPos = 0;
strName.Empty();
strValue.Empty();
while(_T("") != (strText = strLine.Tokenize(szToken, curPos)))
{
if(0 == (nIndex++ % 2))
{
strName = strText;
}
else
{
strValue = strText;
}
}
strMap.insert(make_pair<CString, CString>(strName, strValue));
}
file.Close();

LPCTSTR szPath = _T("F:\\11.ini");
for(map<CString, CString>::const_iterator iter = strMap.begin(); iter != strMap.end(); iter++)
{
WritePrivateProfileString(_T("pathloss"), iter->first, iter->second, szPath);
}
}
catch (CFileException* e)
{
e->ReportError();
e->Delete();
}

Eleven 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wangzf1983 的回复:]
VisualEleven大哥,EXE生成运行成功了。
但是结果不正确,主要是E:\VCpp\IQattn.txt中有很多注释掉的垃圾内容干扰,
而读取的时候照样读取出来了,2412 12.111中间有3个空格。
怎么办呀?您就好人做到底吧,再帮我想想办法吧。时间不急哦!
//E:\VCpp\
//----------------------------------------------……
[/Quote]
囧,你不早说你文件里有注释和其他的东西。。。
过滤掉注释//和类似这样IQ_ATTN_5 = 0就可以了
上面的那个while循环改一下
while(file.ReadString(strLine))
{
curPos = 0;
strName.Empty();
strValue.Empty();
while(_T("") != (strText = strLine.Tokenize(szToken, curPos)))
{
if(0 == (nIndex++ % 2))
{
strName = strText;
}
else
{
strValue = strText;
}
}
strMap.insert(make_pair<CString, CString>(strName, strValue));
}
改成
---》
while(file.ReadString(strLine))
{
curPos = 0;
strName.Empty();
strValue.Empty();
if(-1 != strLine.Find(_T("\\") || -1 != strLine.Find(_T("=")))
{
continue;
}

while(_T("") != (strText = strLine.Tokenize(szToken, curPos)))
{
if(0 == (nIndex++ % 2))
{
strName = strText;
}
else
{
strValue = strText;
}
}
strMap.insert(make_pair<CString, CString>(strName, strValue));
}
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
请楼上大侠教我:
如何从E:\VCpp\IQattn.txt中读取出
Value2412Str,...Value2484Str,...Value4920Str,...Value5945Str(暂时认为读出来为String)
读出来所有频率项的值后(String格式)
,后面写的部分,小弟就自己写的,
前面部分实在不会哦,见笑,恳请帮忙!
洗洗睡去 2010-06-17
  • 打赏
  • 举报
回复
反正格式是固定的 读进之后不管它 就可以了
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
频率项后面3个空格哦,然后是功率值。
实际上E:\VCpp\IQattn.txt还有很多我不需要的文本哦,而且还有后面的4920-5945的38项哦。
详细内容如下:
//------------------------------------------------------------------------------
// NOTE: Please set the nominal value in the calsetup.txt file. IQattn.txt
// should only be used to set incremental changes over the bands.
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Supplemental Attenutation Settings for 2.4 GHz band - up to 50 frequencies.
//------------------------------------------------------------------------------

IQ_ATTN_ENABLED = 0

IQ_ATTN_2p4 = 1

// Supplemental Attenuator value (dB)
2412 12.248
2417 12.319
2422 12.332
2427 12.324
2432 12.292
2437 12.243
2442 12.191
2447 12.118
2452 12.152
2457 12.18
2462 12.218
2467 12.284
2472 12.352
2484 12.426

IQ_ATTN_2p4 = 0

//------------------------------------------------------------------------------
// Supplemental Attenutation Settings for 5 GHz band - up to 50 frequencies.
//------------------------------------------------------------------------------

IQ_ATTN_5 = 1

4920 14.299
4940 14.407
4960 14.005
4980 14.172
5040 14.285
5060 14.325
5080 14.006
5100 14.114
5180 14.287
5200 14.189
5220 14.686
5240 14.288
5260 13.957
5280 14.154
5300 14.191
5320 14.629
5500 14.104
5520 14.098
5540 14.357
5560 14.157
5580 14.309
5600 14.453
5620 14.218
5640 14.404
5660 14.385
5680 14.073
5700 14.027
5745 14.337
5765 14.494
5785 14.333
5805 14.381
5825 14.879
5845 14.51
5865 14.143
5885 14.218
5905 14.562
5925 14.731
5945 14.47

IQ_ATTN_5 = 0


//------------------------------------------------------------------------------
// END of file
//------------------------------------------------------------------------------
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
楼上的是先把所有数据都读出来放在CString str里面。
可是怎么分别定位读出:
StrValue2412,...StrValue2484,...StrValue4920Str,...StrValue5945?
而且2412(频率项)和12.111(值)中间有3个空格。
洗洗睡去 2010-06-17
  • 打赏
  • 举报
回复
其实你的实现代码 最多20行 就可以 你自己写写 不是很好么
successful_cy 2010-06-17
  • 打赏
  • 举报
回复
CString ReadDate(CString fileName)//读数据
{
CString str;
CFile file(fileName,CFile::modeRead);
char *pBuf;
DWORD dwFileLen;
dwFileLen=file.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
file.Read(pBuf,dwFileLen);
file.Close();
str=pBuf;
return str;
}
void WriteData(CString fileName, CString fileStr)//写数据
{
CFile pFile(fileName,CFile::modeNoTruncate|CFile::modeReadWrite);
pFile.Seek(0,CFile::end );
pFile.Write(fileStr,fileStr.GetLength());
pFile.Write("\r\n",strlen("\r\n"));
pFile.Close();

}
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
在线等,自己先顶,增加点人气。
烦请大侠代码写详细点,我笨鸟。。。
洗洗睡去 2010-06-17
  • 打赏
  • 举报
回复
格式这么明显的txt 很好读取啊 cstdiofile 然后按空格分
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
VisualEleven大哥,EXE生成运行成功了。
但是结果不正确,主要是E:\VCpp\IQattn.txt中有很多注释掉的垃圾内容干扰,
而读取的时候照样读取出来了,2412 12.111中间有3个空格。
怎么办呀?您就好人做到底吧,再帮我想想办法吧。时间不急哦!
//E:\VCpp\
//------------------------------------------------------------------------------
// NOTE: Please set the nominal value in the calsetup.txt file. IQattn.txt
// should only be used to set incremental changes over the bands.
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Supplemental Attenutation Settings for 2.4 GHz band - up to 50 frequencies.
//------------------------------------------------------------------------------
IQ_ATTN_ENABLED = 0
IQ_ATTN_2p4 = 1
// Supplemental Attenuator value (dB)
2412 12.111
2417 12.222
2422 12.333
2427 12.444
2432 12.555
2437 12.666
2442 12.191
2447 12.118
2452 12.152
2457 12.18
2462 12.218
2467 12.284
2472 12.352
2484 12.999
IQ_ATTN_2p4 = 0
//------------------------------------------------------------------------------
// Supplemental Attenutation Settings for 5 GHz band - up to 50 frequencies.
//------------------------------------------------------------------------------
IQ_ATTN_5 = 1
4920 14.111
4940 14.407
4960 14.005
4980 14.172
5040 14.285
5060 14.325
5080 14.006
5100 14.114
5180 14.287
5200 14.189
5220 14.686
5240 14.288
5260 13.957
5280 14.154
5300 14.191
5320 14.629
5500 14.104
5520 14.098
5540 14.357
5560 14.157
5580 14.309
5600 14.453
5620 14.218
5640 14.404
5660 14.385
5680 14.073
5700 14.027
5745 14.337
5765 14.494
5785 14.333
5805 14.381
5825 14.879
5845 14.51
5865 14.143
5885 14.218
5905 14.562
5925 14.731
5945 14.999
IQ_ATTN_5 = 0

//------------------------------------------------------------------------------
// END of file
//------------------------------------------------------------------------------
liuyang_1990 2010-06-17
  • 打赏
  • 举报
回复
每天接分,以示存在!!
Eleven 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wangzf1983 的回复:]
10楼的大侠,我用VS2008,Visual C#,Windows,Windows窗体应用程序,
或者VS2008,Visual C++,Windows窗体应用程序,
在窗体上放个Button1,然后运行您的程式,
CString strLine(_T(""));
CString strText(_T(""));
运行报错呀!
个人初学VS2008,最好用Visual C#,Windo……
[/Quote]
用VS2008建个基于对话框的MFC应用程序
wangzf1983 2010-06-17
  • 打赏
  • 举报
回复
10楼的大侠,我用VS2008,Visual C#,Windows,Windows窗体应用程序,
或者VS2008,Visual C++,Windows窗体应用程序,
在窗体上放个Button1,然后运行您的程式,
CString strLine(_T(""));
CString strText(_T(""));
运行报错呀!
个人初学VS2008,最好用Visual C#,Windows,Windows窗体应用程序。
望指点!

16,472

社区成员

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

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

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