怎么读取txt中一列的数字,然后保存到数组中? 类似这样的txt文档,要怎么读取啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主你好 下面的文档供你参考http://zhidao.baidu.com/question/687790914115405644.html 单纯用cfile或者C语言里的读取可以吗?我新手,看起来有点费劲 CFile一次读取到CString对象中,然后利用CString::Tokenize()以\r\n作为分隔符来拆分字符串得到各个子串信息 都可以啊http://blog.csdn.net/lh3325251325/article/details/4761575 都可以啊http://blog.csdn.net/lh3325251325/article/details/4761575有参考作用,但是我没法把cstring转成double数组,最后我是要把TXT的数据存入double数组使用才行 试试: CStdioFile cf; CString OneLine; double db[100]; int idx=0; if(cf.Open("tmp.txt",CFile::modeRead | CFile::typeText)) { while (cf.ReadString(OneLine)) { sscanf(OneLine,"%f",db[idx]); } cf.Close(); } 1 (LPSTR)(LPCSTR) OneLine2 OneLine.GetBuffer(OneLine.); 1 (LPSTR)(LPCSTR) OneLine2 OneLine.GetBuffer(OneLine.GetLength( )); 1 sscanf((LPSTR)(LPCSTR) OneLine,"%f",db[idx]);2 sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%f",db[idx]); 少了一句sscanf((LPSTR)(LPCSTR) OneLine,"%f",db[idx]);idx++;idx 不能超过 99, 因为 double db[100]; 只有100 个 CStdioFile cf;CString OneLine; double db[100]; int idx=0;if(cf.Open("tmp.txt",CFile::modeRead | CFile::typeText)){while (cf.ReadString(OneLine)){sscanf((LPSTR)(LPCSTR) OneLine,"%f",db[idx]);idx++;}cf.Close();} vc6 上 没问题的 语句 !那就用2 sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%f",db[idx]); 你的 是 UNCODE 版本 !sscanf, swscanfRead formatted data from a string.int sscanf( const char *buffer, const char *format [, argument ] ... );int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] ... );Routine Required Header Compatibility sscanf <stdio.h> ANSI, Win 95, Win NT swscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction // 查MSDN,sscanf,找到unicode版本,或者 ANSI和unicode都可用的那个宏 swscanf((LPCWSTR)OneLine.GetBuffer(OneLine.GetLength( )),L"%f",db[idx]); 我把vs的配置环境改成多字符了,然后调用那个数字用teechart显示绘图,出来以后是一条直线。。 我把vs的配置环境改成多字符了,然后调用那个数字用teechart显示绘图,出来以后是一条直线。。 你的txt到底是什么数据 ?point ? 看看 double db[100]; 里 东西 对不对 , (如果 函数 外 用 加 改为 全局变量 !)while 里 sscanf 后加一句afxDump << db[idx]; "加了这句" 在 debug 输出 窗口 看 数字对不对。 point 为什么 不是 一行 一个 x,y ?而且 “222751” 也不是 浮点数, 是 整数 ?仔细看看 sscanf 的 format 和 type sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%f",db[idx]);%f 是浮点格式,改为int data[100]; sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%d",data[idx]); public byte[string] ReadTxt() { List<string> result = new List<string>(); try { using (StreamReader streamReader = new StreamReader(this.FilePath, this.EncodingType)) { while (streamReader.Peek() > 0) { result.Add(streamReader.ReadLine()); } } } catch { } return result.ToArray(); } 这个是我项目中的代码一个参数是 你txt的路径,一个是编码格式,你写txt是哪种 那么你读取的时候也是哪种,如果你不知道哪种就default或者一个个试过来UTF8 UTF7 ASC.等待 用fgets,sscanf实现。 多语言msi安装包制作 如下界面用什么控件实现,非常感谢 Clistctrl删除行问题 我想让一个编辑框获得输入焦点,为何GetDlgItem(IDC_USERNAME)->SetFocus();没用? 急问,在线等待:在基于对话框的程序中的CClientDC使用问题 高手看过来,放分1000 为什么我的程序不能static编译?高分求教 如何对CListCtrl类按照列来排序??(在线等待) 谁用VC编过工业自动化程序? 有人用过VC7.0吗? VC Flash32_20_0_0_267加载flash失败,怎么彻底解决这个问题? 任务栏自动隐藏/显示的消息如何捕获
下面的文档供你参考
http://zhidao.baidu.com/question/687790914115405644.html
http://blog.csdn.net/lh3325251325/article/details/4761575
http://blog.csdn.net/lh3325251325/article/details/4761575有参考作用,但是我没法把cstring转成double数组,最后我是要把TXT的数据存入double数组使用才行
CStdioFile cf;
CString OneLine;
double db[100];
int idx=0;
if(cf.Open("tmp.txt",CFile::modeRead | CFile::typeText))
{
while (cf.ReadString(OneLine))
{
sscanf(OneLine,"%f",db[idx]);
}
cf.Close();
}
2 OneLine.GetBuffer(OneLine.);
2 OneLine.GetBuffer(OneLine.GetLength( ));
2 sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%f",db[idx]);
sscanf((LPSTR)(LPCSTR) OneLine,"%f",db[idx]);
idx++;idx 不能超过 99, 因为 double db[100]; 只有100 个
CString OneLine;
double db[100];
int idx=0;
if(cf.Open("tmp.txt",CFile::modeRead | CFile::typeText))
{
while (cf.ReadString(OneLine))
{
sscanf((LPSTR)(LPCSTR) OneLine,"%f",db[idx]);
idx++;
}
cf.Close();
}
那就用
2 sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%f",db[idx]);
Read formatted data from a string.int sscanf( const char *buffer, const char *format [, argument ] ... );int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] ... );Routine Required Header Compatibility
sscanf <stdio.h> ANSI, Win 95, Win NT
swscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction
while 里 sscanf 后加一句
afxDump << db[idx];
而且 “222751” 也不是 浮点数, 是 整数 ?
仔细看看 sscanf 的 format 和 type
%f 是浮点格式,改为
int data[100];
sscanf(OneLine.GetBuffer(OneLine.GetLength( )),"%d",data[idx]);
{
List<string> result = new List<string>();
try
{
using (StreamReader streamReader = new StreamReader(this.FilePath, this.EncodingType))
{
while (streamReader.Peek() > 0)
{
result.Add(streamReader.ReadLine());
}
}
}
catch
{ }
return result.ToArray();
}
一个参数是 你txt的路径,一个是编码格式,你写txt是哪种 那么你读取的时候也是哪种,如果你不知道哪种就default或者一个个试过来UTF8 UTF7 ASC.等待