C++读txt中的数字

leopard19891022 2011-11-01 08:44:13
怎么把txt中的数字以int 或 float或double 的方式读到一个数组里?
我看网上有用ifstream>>dex的,我试了一下,但是只能读取一个,循环不好使,怎么才能以数字的方式读呢?
实在抱歉,系统最高只让我给30分
...全文
502 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
leopard19891022 2011-11-08
  • 打赏
  • 举报
回复
//#include <sstream>
//#include <fstream>
//#include <iostream>
//#include <vector>
//#include<gl/glut.h>
//using namespace std;
//
//int getFileColumn(/*const */ifstream &in, const string &fileName)
//{
// in.open(fileName.c_str());
// int count = 0;
// string str1;
// while(getline(in, str1))
// count++;
// in.close();
// in.clear();
// return count;
//}
//
//void display(void)
//{
// string str = "";
// ifstream infile;
// float tmp = 0;
//
// vector<vector<float>> vec;
// int row = getFileColumn(infile, "fern.txt");
// vec.resize(row);
// infile.open("fern.txt");
// row = 0;
// while(getline(infile, str))
// {
// istringstream ssm(str);
// while(ssm>>tmp)
// {
// vec.at(row).push_back(tmp);
// }
// row++;
// }
// infile.close();
// infile.clear();
// //-------------------------------------------------------------显示
// //for(vector<float>::size_type i = 0; i < vec.size(); i++)
// //{
// // for(vector<float>::size_type j = 0; j < vec.at(i).size(); j++)
// // cout<<vec[i][j]<<" ";
// // cout<<endl;
// //}
}
leopard19891022 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 keiy 的回复:]

这个要看你原始txt文件的格式了
如:
txt中为
1 2 3 4 5
你就可以用以下程序:
C/C++ code

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n[5];
int i=0;
ifstream fin("d:\\temp\\t.txt");

while(……
[/Quote]
你说对了,我有一个同学是用stringstream做出来的。
lc19881029 2011-11-02
  • 打赏
  • 举报
回复
先讲文本的里面的东西转为string,再用vector和字符串的函数来处理
greymemory 2011-11-02
  • 打赏
  • 举报
回复
学习了。帮顶!!!!
lord_is_layuping 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yyg990441 的回复:]

引用 2 楼 keiy 的回复:
这个要看你原始txt文件的格式了
如:
txt中为
1 2 3 4 5
你就可以用以下程序:

C/C++ code
int i=0;
ifstream fin("d:\\temp\\t.txt");

while(!fin.eof())
fin >> n[i++];
这个程序有问题吧?假设文件为空,以开始fin.eof()也是为fal……
[/Quote]

帮顶,学习
yyg990441 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 keiy 的回复:]
这个要看你原始txt文件的格式了
如:
txt中为
1 2 3 4 5
你就可以用以下程序:

C/C++ code
int i=0;
ifstream fin("d:\\temp\\t.txt");

while(!fin.eof())
fin >> n[i++];[/Quote]
这个程序有问题吧?假设文件为空,以开始fin.eof()也是为false的,所以你这个程序多读了一个数
mskmc_mc 2011-11-01
  • 打赏
  • 举报
回复
ifstream file("text.txt");
int t [1000];
int i=0;
int i1

while(i1=fille.get())
{if(i1>47&&i1<58)
t[i++]=i1;}

酬和着看吧

具体的还有很多

比如:先用空格过滤文本中的非数字
再用4楼的方法就可以了
yyg990441 2011-11-01
  • 打赏
  • 举报
回复

/*data.txt内容
12.5 3.4 5.6
7.7 9 1000 33.7
*/
#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

int main(){
ifstream in("data.txt");
typedef istream_iterator<double> IsIT;
vector<double> vec;
copy( IsIT(in), IsIT(), back_inserter(vec) );
copy( vec.begin(),vec.end(), ostream_iterator<double>(cout," ") );
}
lz0300 2011-11-01
  • 打赏
  • 举报
回复
字母是两位数字吧?那么就可以把a到z抛弃,
然后统计文件字符数合计,来个循环!
再输出就是数字了吧!再下来……

柯本 2011-11-01
  • 打赏
  • 举报
回复
这个要看你原始txt文件的格式了
如:
txt中为
1 2 3 4 5
你就可以用以下程序:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n[5];
int i=0;
ifstream fin("d:\\temp\\t.txt");

while(!fin.eof())
fin >> n[i++];

for(i=0;i<5;i++)
cout << n[i] <<endl;
return 0;
}
turing-complete 2011-11-01
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/mougaidong/3701724
读成字符串再转化,没有那么智能的功能

65,180

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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