C++ 将结构体中的数据存入文件 哪位大侠棒棒我,小妹谢谢!急啊!好的话,再加分!

作者&投稿:塞费 (若有异议请与网页底部的电邮联系)
c++程序有错,哪位大侠能帮忙改一下啊?非常感谢!!!~

改的地方加了注释
#include
using namespace std;
int main()
{
int i;
int flag=0;
void sizeyunsuan();
void zhengshubei();
void sum();
void max();
void sushu();
cout<<" c++程序综合应用"<<endl;
cout<<" ==============================================="<<endl;
cout<<"(1):两整数的四则运算;"<<endl;
cout<<"(2):判断一整数是否既是5又是7的整数倍;"<<endl;
cout<<"(3):求分数序列1+1/3+1/5+1/7+……+1/n之和;"<<endl;
cout<<"(4):找出n个数中的最大数并输出;"<<endl;
cout<<"(5):输出100~200之间的素数;"<<endl;
cout<<"(6):退出程序。"<<endl;
cout<<"======================================"<<endl;
cout<<"请选择:"<<endl;
do
{

cin>>i;
switch(i)
{
case 1 : sizeyunsuan(); break;
case 2 : zhengshubei(); break;
case 3 : sum(); break;
case 4 : max(); break;
case 5 : sushu(); break;
case 6 : flag=1; break;
default:cout<<"您的输入错误,请重新输入"<<endl;
}//加个大括号和switch配对
}while(flag==0);//注意==和=的用法
system("pause");
}

void sizeyunsuan()//去掉分号
{
float a,b;
cin>>a>>b;
cout<<"a+b="<<a+b<<endl;
cout<<"a-b="<<a-b<<endl;
cout<<"a*b="<<a*b<<endl;
cout<<"a/b="<<a/b<<endl;
}
void zhengshubei()
{
int a;
cin>>a;
if(a%5==0 && a%7==0)//注意==和=的用法
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
void sum()
{
int m,n;
int s=0;
cout<<"请输入最后一个分数的分母:"<<endl;
cin>>m;
for(n=1;n<=m;n+2)
{
s=s+1/n;
}
cout<<"分数序列1+1/3+1/5+1/7+……+1/n之和为"<<s<<endl;
}
void max()
{
float m;
float a[100];
int j,n;
cout<<"请问您要输入多少个数?"<<endl;
cin>>n;
cout<<"请输入这些数"<<endl;
for(j=0;j<n;j++)
{
cin>>a[j];
}
for(j=0;j<n;j++)
{
m=a[0];
if(m<a[j])
m=a[j];
}
cout<<"最大的数是:"<<m;
}
void sushu()
{
int num=100;
int i;
bool flag;
while( num<= 200 )
{
flag = true;
for( i = 2; i<num; i++)
{
if(num%i == 0)
{
flag = false;
break;
}
}
if(flag)
cout<<num<<"";
num ++;
}
}

我建议你用VS2010试着用来进行C++程序的编写,计算机行业嘛,要与时俱进,当然了VS系列的软件都是windows下的程序开发。

#include <fstream> // 文件流头文件.
#include <iostream>
using namespace std;

struct ST // 你的结构
{
string name;
int age;
int score;

};

ostream& operator <<(ostream& os, const ST& st){
os << st.name << " " << st.age << " " << st.score; // 输出流, 依次输出成员, 用空格隔开
return os;
}

istream& operator >>(istream& is, ST& st){
is >> st.name >> st.age >> st.score; // 输入流
return is;
}
// 现在就可以使用流了.

int main( ){
ofstream ofs("D:\\123.txt"); // ofstream 是C++文件输出流
ST st; // 一个临时变量

cout << "请输入 名字 年龄 分数" << endl;
cin >> st; // 读取用户的输入. 这里调用了前面定义的operator >>

ofs << st << endl; // 把st保存到文件中.
ofs.close(); // 关闭文件

ifstream ifs("D:\\123.txt"); // 还是打开刚才的文件, ifstream是文件输入流
ifs >> st; // 从文件中读取一个st数据.
cout << st << endl; // 把它输出到控制台

ifs.close(); // 关闭文件
return 0;
}

主要就在于开头的重载ST特定的 >> 和 <<

#include "stdafx.h"#include "stdio.h"
FILE *pStream;
void main()
{
// WriteData();
int b[10] = {0};
if( (pStream = fopen( "E:/data.txt", "r" )) != NULL )
{
fread(b, sizeof(int), 10, pStream);
fclose(pStream);
}

if( (pStream = fopen( "E:/data2.txt", "w" )) != NULL )
{
fwrite(b, sizeof(int), 10, pStream);
fclose(pStream);
}
}

void WriteData()
{
int a[10] = {1, 2, 4, 7, 8, 9, 10, 12, 13, 14};
if( (pStream = fopen( "E:/data.txt", "w" )) != NULL )
{
fwrite(a, sizeof(int), 10, pStream);
fclose(pStream);
}
}

刚给别人写的 你的和这一样,只是把数组换成结构


大石桥市15926603641: C++结构体问题,如何把一个结构体写入文件 -
叱干绿盐酸: #include<iostream> #include <fstream>using namespace std;struct SalaryInfo{unsigned id;double salary;//char name[23]; }; int main(){SalaryInfo employee1 ={600001, 8000};//, "F Lei"};ofstream os("payroll", ios_base::out | ios_base::...

大石桥市15926603641: C++ 能将结构体任意的写入文本文件? -
叱干绿盐酸: 您好,以下是个例子: #include<iostream> #include <fstream>using namespace std;struct SalaryInfo{unsigned id;double salary;//char name[23]; };int main(){SalaryInfo employee1 ={600001, 8000};//, "F Lei"};ofstream os("payroll", ios...

大石桥市15926603641: c++定义一个结构体类型的变量,将其写到文本文件 -
叱干绿盐酸: struct A { int i; float f; std::string str; }; template<typename Char, typename Traits> std::basic_ostream<Char, Tratis>& operator<<(std::basic_ostream<Char, Traits>& os, const A& a) { os << a.i << a.f << a.str; return os; } int main(void) { A a; // init a std...

大石桥市15926603641: C语言文件读写结构体里面的数据怎样存到磁盘文件上 -
叱干绿盐酸: 1、首先打开VC++6.0. 2、选择文件,新建. 3、选择C++ source file 新建一个空白文档. 4、首先声明头文件,#include<stdio.h>. 5、主函数,定义变量main( ){ FILE *fp; int i; 需要一个结构体,struct rec{ /*定义结构体类型*/ char id[10];. 6、主要代码if( (fp=fopen("d:\\infile.txt","w"))==NULL ) /*以文本只写方式打开文件*/ { printf("cannot open file"); exit(1); }.

大石桥市15926603641: C++如何读入一个文件中的结构体数组数据? -
叱干绿盐酸: 取决于文件中数据的存储方式.1 如果文件中存储的方式为二进制形式数据:需要使用fread(C语言风格)或ifsteam的read成员函数(C++风格.)从文件中读取结构体数据到对应的结构体指针上.如 struct test { int a; }; struct test t; fread(&t, 1,sizeof(t...

大石桥市15926603641: 结构体写入数组文件 c++ -
叱干绿盐酸: 楼上错了 应该是这样 FILE *file=fopen("d:\\abc.txt","w"); fwrite(&dep,sizeof(Depositor),1,file);

大石桥市15926603641: 在C++中怎么实现对结构体数组的数据保存,和对保存数据的读取? -
叱干绿盐酸: 用ini文件保存结构体中的每个变量值或者用CFile 类进行行保存,如果不要求用文本文件的话还可以直接序列化到二进制文件中,当需要读取时再进行反序列化就可以了

大石桥市15926603641: C++怎么把数据保存到一个文件里 -
叱干绿盐酸: 定义写入的时候指定格式,例如: ofstream out("out.txt",ios::out | ios::app); //这样就可以实现追加添加啦ios_base::in“r”打开一读取ios_base::out“w”等价于ios_base::out|ios_base::trunk(有时可以省略)out|trunc“w”打开一...

大石桥市15926603641: 【c++】包含string类的结构体类型二进制文件读入输出 -
叱干绿盐酸: // 不知道你的 KeyType 是什么类型就把他省掉了.// 如果用C的话记得把输出语句改成printf;#include <stdio.h>#include <string>#include <iostream> using namespace std; struct ElemType{ string content; //内容域 int num; };// 无论你结构体里...

大石桥市15926603641: C++中将数据存入文件的问题 -
叱干绿盐酸: 需要包涵文件输入的头文件,即:#include<fstream>,然后定义一个文件输入的关联,通俗的讲就是“变量”.给你举个例子吧#include<iostream> //在我下面的代码中可以不要#include<fstream> using namespace std; int main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; ofstream fout("fuck.txt",ios::out); for(int i=0;i<10;i++) fout<<a[i]<<endl; return 0; } 就是这样,希望对你有帮助~

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网