读取txt文件中的负数???

erlingmusan 2011-03-06 03:21:04
U.txt中的数据
-721.687179 -815.804502 -811.202694 -789.820228 -819.852144 -892.327928 -855.463031 -896.633812 -883.067909 -854.481898 -827.922019 -881.006647 -834.705544 -823.172092 -806.361069 -817.773490 -838.073045 -839.921275 -928.714609 -837.331740 -816.831521
-20.263213 -3.191537 38.048945 34.034564 38.551518 -9.860831 48.968289 47.625681 37.903990 67.662095 -39.756499 -32.747018 -61.186375 -12.614137

怎么把U.txt中的数据读到一个二维数组中,里面有负数,不知道怎么读?
...全文
1038 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
delphiwcdj 2011-03-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qq120848369 的回复:]

C语言方法fscanf("%lf"),C++方法如上。
[/Quote]
++
pmars 2011-03-06
  • 打赏
  • 举报
回复
读取负数和正数没有什么区别,写一样的程序,让编译器去工作吧……
qq120848369 2011-03-06
  • 打赏
  • 举报
回复
C语言方法fscanf("%lf"),C++方法如上。
dingshaofengbinbin 2011-03-06
  • 打赏
  • 举报
回复
不就是字符转换吗??
hongwenjun 2011-03-06
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main()
{
double x;
vector<double> shuzi;
vector<double>::iterator iter;
fstream fin;
fin.open("a.txt");
if(!fin) {
cout<<"不能打开文件"<<endl;
return -1;
}

while(fin >> x) {
shuzi.push_back(x);
}

sort(shuzi.begin(),shuzi.end());
for (iter=shuzi.begin(); iter!=shuzi.end() ; iter++ )
cout << *iter << endl;
fin.close();

cout <<"总共数字"<< shuzi.size();
return 0;
}

thegreatone 2011-03-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
ifstream infile;
infile.open("U.txt",ios::in);
if(!infile)
{
cout<<"fail to open the file source.txt"<<endl;
exit(1);
}
double data;
while(infile>>data)
{
cout<<data<<" ";
}
infile.close();

return 0;
}

thegreatone 2011-03-06
  • 打赏
  • 举报
回复
直接用读入double类型的变量中就行了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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