Home > Back-end >  C from the text to read each line is a struct, and then each line in the vector, the output to the s
C from the text to read each line is a struct, and then each line in the vector, the output to the s

Time:09-19



Document A, each line is A struct, finally the number of Numbers is not necessarily need to put each line in the vector and then output to the screen, and also exported to the new document B, give advice or comments, thank you!

Struct hero
{
string name;
int age;
Char sex;
double weight;
The string color;
Vector number;
};

CodePudding user response:

Could not find your question, you is to ask people to help you accomplish this task? What is the problem or task encounters?

CodePudding user response:

//CPP file read and write. CPP: defines the entry point of the console application, 
//

# include "stdafx. H"
#include
#include
#include
#include
#include
using namespace std;

Const string inFileName="testData. TXT";
Const string outFileName="outData. TXT";

Struct Hero
{
string name;
int age;
Char sex;
double weight;
The string color;
Vector number;
};

Vector HeroList;

Int _tmain (int arg c, _TCHAR * argv [])
{
Ifstream fin;
Fin. Open (inFileName. C_str (), the ios: : in);
if(! Fin. Is_open ())
{
Cout<" Open the file "& lt; exit(1);
}

Ofstream fout.
Fout. Open (outFileName. C_str (), the ios: : out);
if(! Fout. Is_open ())
{
Cout<" Create a file "& lt; exit(1);
}

The string line;
Getline (fin, line);
While (line. The length () & gt; 0)//& gt; 20
{
Istrstream ss (line c_str ());
Hero aHero;
Ss> AHero. Name> AHero. Age> AHero. Sex> AHero. Weight> AHero. Color;
CoutFout
Int num.
Do
{
Ss> Num.
AHero. Number. The push_back (num);
Cout<'\ t' & lt; Fout<'\ t' & lt; } while (ss) good ());

//ss. The clear ();
CoutFoutHeroList. Push_back (aHero);
Getline (fin, line);
}

fin.close();
fout.close();
return 0;
}

CodePudding user response:

Modify the,
While (line. The length () & gt; 0)//& gt; 8 minimum length may be 9, including five fields, 4 Tab characters or Spaces
And, behind the number of uncertain may be zero, changing the while (ss) good ())
//CPP file read and write. CPP: defines the entry point of the console application, 
//

# include "stdafx. H"
#include
#include
#include
#include
#include
using namespace std;

Const string inFileName="testData. TXT";
Const string outFileName="outData. TXT";

Struct Hero
{
string name;
int age;
Char sex;
double weight;
The string color;
Vector number;
};

Vector HeroList;

Int _tmain (int arg c, _TCHAR * argv [])
{
Ifstream fin;
Fin. Open (inFileName. C_str (), the ios: : in);
if(! Fin. Is_open ())
{
Cout<" Open the file "& lt; exit(1);
}

Ofstream fout.
Fout. Open (outFileName. C_str (), the ios: : out);
if(! Fout. Is_open ())
{
Cout<" Create a file "& lt; exit(1);
}

The string line;
Getline (fin, line);
While (line. The length () & gt; 0)//& gt; 8
{
Istrstream ss (line c_str ());
Hero aHero;
Ss> AHero. Name> AHero. Age> AHero. Sex> AHero. Weight> AHero. Color;
CoutFout
Int num.
While (ss) good ())
{
Ss> Num.
AHero. Number. The push_back (num);
Cout<'\ t' & lt; Fout<'\ t' & lt; }

//ss. The clear ();
CoutFoutHeroList. Push_back (aHero);
Getline (fin, line);
}

fin.close();
fout.close();
return 0;
}

CodePudding user response:

Should use the
Istringstream ss (line);
Replace
Istrstream ss (line c_str ());
Header file by strstream into sstream
Better still, use not familiar with impression all mixed up,
  • Related