刚刚学c++,发一个数据文件读与写的有关问题
刚刚学c++,发一个数据文件读与写的问题
struct
{
float r;
float Ls;
float a;
}rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
这样一个结构体数组,怎么样把它写到磁盘文件上,怎么样读取,希望能告诉我代码,万分感谢
------解决方案--------------------
#include "fstream "
using namespace std;
void main() {
struct
{
float r;
float Ls;
float a;
} rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
ofstream file( "text.txt ");
if (file.fail())
return;
for (int i = 0; i < sizeof(rdata) / sizeof(*rdata); i++) {
file < <rdata[i].r < <endl;
file < <rdata[i].Ls < <endl;
file < <rdata[i].a < <endl;
}
file.close();
}
------解决方案--------------------
ofstream out( "output.txt ");
for (int i=0; i <5; i++)
{
out.width(10);
out < <rdata[i].r;
out.width(10);
out < <rdata[i].Ls;
out.width(10);
out < <rdata[i].a;
out < <endl;
}
out.close();
------解决方案--------------------
#include <fstream>
using namespace std;
struct
{
float r;
float Ls;
float a;
}rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
void main()
{
ofstream outfile( "out.txt ");
for(int i=0;i <5;i++)
{
outfile < <rdata[i].r < < " " < <rdata[i].Ls < < " " < <rdata[i].a < <endl;
}
看看c++ primer吧
struct
{
float r;
float Ls;
float a;
}rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
这样一个结构体数组,怎么样把它写到磁盘文件上,怎么样读取,希望能告诉我代码,万分感谢
------解决方案--------------------
#include "fstream "
using namespace std;
void main() {
struct
{
float r;
float Ls;
float a;
} rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
ofstream file( "text.txt ");
if (file.fail())
return;
for (int i = 0; i < sizeof(rdata) / sizeof(*rdata); i++) {
file < <rdata[i].r < <endl;
file < <rdata[i].Ls < <endl;
file < <rdata[i].a < <endl;
}
file.close();
}
------解决方案--------------------
ofstream out( "output.txt ");
for (int i=0; i <5; i++)
{
out.width(10);
out < <rdata[i].r;
out.width(10);
out < <rdata[i].Ls;
out.width(10);
out < <rdata[i].a;
out < <endl;
}
out.close();
------解决方案--------------------
#include <fstream>
using namespace std;
struct
{
float r;
float Ls;
float a;
}rdata[5]={{250,70,38},{300,65,36},{350,60,34},{400,55,32},{450,50,30}};
void main()
{
ofstream outfile( "out.txt ");
for(int i=0;i <5;i++)
{
outfile < <rdata[i].r < < " " < <rdata[i].Ls < < " " < <rdata[i].a < <endl;
}
看看c++ primer吧