
檔案A中每一行都是一個struct,最后幾個數字的數量是不一定的,需要把每一行都放入vector然后輸出到螢屏,并且也輸出到新建的檔案B中。求指教,謝謝!
struct hero
{
string name;
int age;
char sex;
double weight;
string color;
vector<int> number;
};
uj5u.com熱心網友回復:
沒有找到你提的問題,你是要請人幫你完成這個任務?還是任務中碰到了什么問題?uj5u.com熱心網友回復:
// 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;
}
uj5u.com熱心網友回復:
修改了一下,while(line.length()>0) //>8 最小長度可能是9,包括5個欄位,4個Tab字符或者空格
還有,后面不定數量的數字可能是0個,改成 while(ss.good())
// 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;
}
uj5u.com熱心網友回復:
應該用istringstream ss(line);
代替
istrstream ss(line.c_str());
頭檔案由strstream 改成 sstream
更好一點,用的不熟憑印象都混了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/52026.html
標籤:基礎類
上一篇:學生資訊系統管理的研究
