13,822
社区成员
发帖
与我相关
我的任务
分享
// CPP文件读写.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <vector>
using namespace std;
const string inFileName="testData.txt";
const string outFileName="outData.txt";
struct Hero
{
string name;
int age;
char sex;
double weight;
string color;
vector<int> number;
};
vector<Hero> heroList;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream fin;
fin.open(inFileName.c_str(),ios::in);
if(!fin.is_open())
{
cout<<"打开文件"<<inFileName<<"失败。"<<endl;
exit(1);
}
ofstream fout;
fout.open(outFileName.c_str(),ios::out);
if(!fout.is_open())
{
cout<<"创建文件"<<outFileName<<"失败。"<<endl;
exit(1);
}
string line;
getline(fin,line);
while(line.length()>0) //>20
{
istrstream ss(line.c_str());
Hero aHero;
ss>>aHero.name>>aHero.age>>aHero.sex>>aHero.weight>>aHero.color;
cout<<aHero.name<<'\t'<<aHero.age<<'\t'<<aHero.sex<<'\t'<<aHero.weight<<'\t'<<aHero.color;
fout<<aHero.name<<'\t'<<aHero.age<<'\t'<<aHero.sex<<'\t'<<aHero.weight<<'\t'<<aHero.color;
int num;
do
{
ss>>num;
aHero.number.push_back(num);
cout<<'\t'<<num;
fout<<'\t'<<num;
}while(ss.good());
// ss.clear();
cout<<endl;
fout<<endl;
heroList.push_back(aHero);
getline(fin,line);
}
fin.close();
fout.close();
return 0;
}
// CPP文件读写.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <vector>
using namespace std;
const string inFileName="testData.txt";
const string outFileName="outData.txt";
struct Hero
{
string name;
int age;
char sex;
double weight;
string color;
vector<int> number;
};
vector<Hero> heroList;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream fin;
fin.open(inFileName.c_str(),ios::in);
if(!fin.is_open())
{
cout<<"打开文件"<<inFileName<<"失败。"<<endl;
exit(1);
}
ofstream fout;
fout.open(outFileName.c_str(),ios::out);
if(!fout.is_open())
{
cout<<"创建文件"<<outFileName<<"失败。"<<endl;
exit(1);
}
string line;
getline(fin,line);
while(line.length()>0) //>8
{
istrstream ss(line.c_str());
Hero aHero;
ss>>aHero.name>>aHero.age>>aHero.sex>>aHero.weight>>aHero.color;
cout<<aHero.name<<'\t'<<aHero.age<<'\t'<<aHero.sex<<'\t'<<aHero.weight<<'\t'<<aHero.color;
fout<<aHero.name<<'\t'<<aHero.age<<'\t'<<aHero.sex<<'\t'<<aHero.weight<<'\t'<<aHero.color;
int num;
while(ss.good())
{
ss>>num;
aHero.number.push_back(num);
cout<<'\t'<<num;
fout<<'\t'<<num;
}
// ss.clear();
cout<<endl;
fout<<endl;
heroList.push_back(aHero);
getline(fin,line);
}
fin.close();
fout.close();
return 0;
}