首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如果读取文本文件的一部分内容?该怎么处理

2012-02-20 
如果读取文本文件的一部分内容?outerloopvertex-97.30535138.784847-5.195522vertex-92.67366844.794323-6

如果读取文本文件的一部分内容?
outer   loop
        vertex   -97.305351   38.784847   -5.195522
        vertex   -92.673668   44.794323   -6.000128
        vertex   -99.500107   39.165230   -10.090099
  endloop
endfacet
facet   normal   0.811528   -0.570721   0.125300
  outer   loop
        vertex   137.308319   11.651189   1.073192
        vertex   138.194916   13.339867   3.022695
        vertex   135.987549   10.617146   4.917568
  endloop
endfacet
facet   normal   0.146591   -0.154363   -0.977079
  outer   loop
        vertex   -48.739738   24.875885   2.289428
        vertex   -45.140621   22.351807   3.228165
        vertex   -50.495110   17.923044   3.124506
  endloop
endfacet
facet   normal   -0.222846   0.413511   0.882807
  outer   loop
        vertex   24.973944   123.191765   3.822913
        vertex   27.039553   123.236137   4.323550
        vertex   25.572515   125.122276   3.069748
  endloop
endfacet

这是文本文件中存放的东西
如果我只想读取vertex后面的数据的话,我应该如何读取呢
能不能给提供一下代码呢

[解决办法]
试试这个:)

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

//-----------------------------------------
//CLASS:
// Vertex
//DESCRIPTION:
// hold the vertex coordinate
//-----------------------------------------
class Vertex
{
public:
Vertex(double _x=0, double _y=0, double _z=0)
:x_(_x), y_(_y), z_(_z)
{}

Vertex(Vertex const& rhs)
{
x_ = rhs.x_;
y_ = rhs.y_;
z_ = rhs.z_;
}

Vertex& operator=(Vertex const& rhs)
{
if (this!=&rhs) {
x_ = rhs.x_;
y_ = rhs.y_;
z_ = rhs.z_;
}
return *this;
}

double X() { return x_;}
void X(double _x) { x_ = _x;}

double Y() { return y_;}
void Y(double _y) { y_ = _y;}

double Z() { return z_;}
void Z(double _z) { z_ = _z;}

private:
double x_;
double y_;
double z_;
};

typedef vector <Vertex> VertexArray;

//-----------------------------------------
// CLASS:
// VertexReader
// DESCRIPTION
// read the vetex data from a file
//-----------------------------------------
class VertexReader
{
public:
VertexReader(string const& file_name)
:FileName_(file_name)
{}

~VertexReader()
{}

bool Read(VertexArray& vertices);

private:
//no implementations
VertexReader();
VertexReader(VertexReader const& rhs);
VertexReader& operator=(VertexReader const& rhs);



void GetTokens(string& line);
void InterpretTokens(VertexArray& vertices);

vector <string> Tokens_;
string FileName_;
};

bool VertexReader::Read(VertexArray& vertices)
{
ifstream infile(FileName_.c_str(), ifstream::in);
if (!infile) {
cout < < "Error: can 't open the file : " < <FileName_ < <endl;
return false;
}

string line;
while (!infile.eof())
{
if (getline(infile, line)) {
GetTokens(line);
InterpretTokens(vertices);
}
}

return true;
}

void VertexReader::GetTokens(string& line)
{
static char sDel[] = " \t\r\n ";

Tokens_.clear();

size_t nChar = line.length();
if (nChar> =1) {
char* buff = new char[line.length()+1];
strncpy(buff, line.c_str(), line.length());

char* token = strtok(buff, sDel);
while (token) {
Tokens_.push_back(token);
token = strtok(NULL, sDel);
}
delete []buff;
}
}

void VertexReader::InterpretTokens(VertexArray& vertices)
{
unsigned int requiredTokens = 4;
if (Tokens_.size()==requiredTokens) {
if (stricmp(Tokens_[0].c_str(), "vertex ")==0) {
double x = atof(Tokens_[1].c_str());
double y = atof(Tokens_[2].c_str());
double z = atof(Tokens_[3].c_str());
vertices.push_back(Vertex(x, y, z));
}
}
}


//---------------------------
//Entry point to test the VertexReader
//---------------------------
int main(int argc, char** argv)
{
// the location of your file containing vertex data
string fileName( "d:\\temp\\vertex.txt ");

// construct a VertexReader object with the file name
VertexReader reader(fileName);

// construct a vector <Vertex>
VertexArray vertices;

if (reader.Read(vertices)) { // Read the vertex data of the file

// if read success, display the vertices of the file
cout < < "Number of vertex read: " < <vertices.size() < <endl;
for (int i=0; i <vertices.size(); ++i) {

cout < < "Vertex " < <i < < ": ";

cout < < "( "
< <vertices[i].X() < < ", "
< <vertices[i].Y() < < ", "
< <vertices[i].Z()
< < ") "
< <endl;
}
}

system( "pause ");
}

热点排行