C++ 读取TXT

来源:百度知道 编辑:UC知道 时间:2024/07/05 05:03:38
1.0 2.0 3.0 5.9 5.6
20. 3.0 4.0 5.0 6.8
txt内容如上面,用C++读取到数组应该怎么做呀~
谢谢指点

#include <iostream>
#include<fstream>
using namespace std;
void main()
{

char a[40];
int i=0;
fstream openfile("XXX.txt");
if(!openfile)
{
cout<<"open failed!"<<endl;
exit(1);
}
do{
openfile.get(a[i]);
if(openfile.eof())
break;
//cout<<a[2][i];
i++;
}while(!openfile.eof());
for(i=0;i<40;i++)
cout<<a[i];
cout<<endl;

}

格式化读入,比如:a[i][j]=fscanf(fp,“**.txt”,“r”);
赋值语句需要有一个循环来控制,要设计好这个循环是关键,也是最基础的就不用多说了。

不懂接着问。

给你一个简单的读取数组数据的代码,看过之后就明白了,如果不会继续留言吧
txt中的文件:
0 0 0
56.1060077 304.9748347 0.01618219
120.2269187 629.1440333 0.065887285
192.3386393 972.7779839 0.151361829
272.4066549 1336.170242 0.274719419
C++文件:
#include "iostream.h"
#include "fstream.h"
#include "stdlib.h&