how to read it out?

P

pisscot

Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: (e-mail address removed) (Lind) - Find messages by this author
Date: 13 Apr 2005 06:32:15 -0700
Local: Wed,Apr 13 2005 6:32 am
Subject: how to read it out using c++
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

8->line 1
30 150
150 30
150 150
30 30
60 120
120 120
120 60
60 60
0 2 1 -3 4 7 6 -5->line2


this is what I have in my poly.dat
I'm quite confused on how to seperate it
I want to read my line1 data as an integer and line2 data should be
put into array which will be used for later code
and the rest line into multidimensional array because each line
represents an (x,y) of a point.


Can anyone figure this out?
 
C

codigo

Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: (e-mail address removed) (Lind) - Find messages by this author
Date: 13 Apr 2005 06:32:15 -0700
Local: Wed,Apr 13 2005 6:32 am
Subject: how to read it out using c++
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

8->line 1
30 150
150 30
150 150
30 30
60 120
120 120
120 60
60 60
0 2 1 -3 4 7 6 -5->line2


this is what I have in my poly.dat
I'm quite confused on how to seperate it
I want to read my line1 data as an integer and line2 data should be
put into array which will be used for later code
and the rest line into multidimensional array because each line
represents an (x,y) of a point.


Can anyone figure this out?

What you are trying to do is standard practice. That's assuming you've
bothered reading up on std::strings, std::ifstreams, std::vector and the
like. Why not build a Document class that parses the document for you?
Instead of a vector of ints, you could write a class to hold an x and y
coordinate.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;

int main(int argc, char* argv[])
{
// A std::istream opens the specified file:

const string s_file = "poly.dat";
std::ifstream ifs;
ifs.open(s_file.c_str(), std::ios::in);
if(!ifs)
{
// error check or throw
std::cout << "error: can't open file.";
return -1;
}

// then read the first line which represents a record count
// (using std::getline).
// load the record count into n_count.

string s_input;
int n_count;
std::getline(ifs, s_input, '\n');
n_count = atoi(s_input.c_str());

// Now you have the record count

std::vector<int> vn; // container
string s; // temp string
int n_x; // left value
int n_y; // right value
int n_seperator; // space locator for parsing
int n_length; // end locator for parsing

for (int i = 0; i < n_count; i++)
{
std::getline(ifs, s_input, '\n'); // collect data string

// locate the space between numbers and end of data
n_seperator = s_input.find_first_of(' ');
n_length = s_input.length();

// parse s_input, load vector
s = s_input.substr(0, n_seperator);
n_x = atoi( s.c_str() );
vn.push_back(n_x);
s = s_input.substr(n_seperator + 1, n_length);
n_y = atoi( s.c_str() );
vn.push_back(n_y);
}

// display
for (i = 0; i < n_count; i++)
{
std::cout << "x[" << i << "]\t";
std::cout << vn[i * 2];

std::cout << "\ty[" << i << "]\t";
std::cout << vn[i * 2 + 1];
std::cout << "\n";
}

return 0;
} // main
 
L

lind

well , i got a class and can't seem to how to allocate it into the
private
this is the class
class myPoly
{
private:
int nv;
double *x, *y;
int *p;
public:
myPoly()
{
x = new double (0);
y = new double (0);
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;
}

~myPoly()
{
//delete x;
//delete y;
}
and the read function is to read the poly.dat

void Read(const char[]) // name of the datafile
{
char out[sizes];
while(!infile.eof())
{
infile.getline(out,sizes);
strcpy(out2,out);
}
}

Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: (e-mail address removed) (Lind) - Find messages by this author
Date: 13 Apr 2005 06:32:15 -0700
Local: Wed,Apr 13 2005 6:32 am
Subject: how to read it out using c++
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

8->line 1
30 150
150 30
150 150
30 30
60 120
120 120
120 60
60 60
0 2 1 -3 4 7 6 -5->line2


this is what I have in my poly.dat
I'm quite confused on how to seperate it
I want to read my line1 data as an integer and line2 data should be
put into array which will be used for later code
and the rest line into multidimensional array because each line
represents an (x,y) of a point.


Can anyone figure this out?

What you are trying to do is standard practice. That's assuming you've
bothered reading up on std::strings, std::ifstreams, std::vector and the
like. Why not build a Document class that parses the document for you?
Instead of a vector of ints, you could write a class to hold an x and y
coordinate.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;

int main(int argc, char* argv[])
{
// A std::istream opens the specified file:

const string s_file = "poly.dat";
std::ifstream ifs;
ifs.open(s_file.c_str(), std::ios::in);
if(!ifs)
{
// error check or throw
std::cout << "error: can't open file.";
return -1;
}

// then read the first line which represents a record count
// (using std::getline).
// load the record count into n_count.

string s_input;
int n_count;
std::getline(ifs, s_input, '\n');
n_count = atoi(s_input.c_str());

// Now you have the record count

std::vector<int> vn; // container
string s; // temp string
int n_x; // left value
int n_y; // right value
int n_seperator; // space locator for parsing
int n_length; // end locator for parsing

for (int i = 0; i < n_count; i++)
{
std::getline(ifs, s_input, '\n'); // collect data string

// locate the space between numbers and end of data
n_seperator = s_input.find_first_of(' ');
n_length = s_input.length();

// parse s_input, load vector
s = s_input.substr(0, n_seperator);
n_x = atoi( s.c_str() );
vn.push_back(n_x);
s = s_input.substr(n_seperator + 1, n_length);
n_y = atoi( s.c_str() );
vn.push_back(n_y);
}

// display
for (i = 0; i < n_count; i++)
{
std::cout << "x[" << i << "]\t";
std::cout << vn[i * 2];

std::cout << "\ty[" << i << "]\t";
std::cout << vn[i * 2 + 1];
std::cout << "\n";
}

return 0;
} // main
 
L

lind

well , i got a class and can't seem to how to allocate it into the
private
this is the class
class myPoly
{
private:
int nv;
double *x, *y;
int *p;
public:
myPoly()
{
x = new double (0);
y = new double (0);
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;
}

~myPoly()
{
//delete x;
//delete y;
}
and the read function is to read the poly.dat

void Read(const char[]) // name of the datafile
{
char out[sizes];
while(!infile.eof())
{
infile.getline(out,sizes);
strcpy(out2,out);
}
}
well, I want to store the x,y and in the x,y inside the private class
and the line 2 inside the p pointer. and the nv is the times each time
they allowcate a new space.
 
C

codigo

lind said:
well , i got a class and can't seem to how to allocate it into the
private
this is the class
class myPoly
{
private:
int nv;
double *x, *y;
int *p;
public:
myPoly()
{
x = new double (0);
y = new double (0);
nv = 0;
}

Why do you need to handle the allocation? Use an initialization list. i see
no logic that supports the need for new / delete here. Specially since you
would rather new and delete the Poly instance instead.

myPoly(int n) : nv(n), x(0), y(0)
{
}
myPoly(int n) // number of vertices
{
nv = n;
}

~myPoly()
{
//delete x;
//delete y;
}
and the read function is to read the poly.dat

Use std::string instead, this is the 21st century. Think serialize + parse
the getlined strings one at a time instead of one large char array per file
(there is an inherent danger to doing this). Strings and Streams are to
powerful to be ignored, not to mention safe and trivial to implement.

Also, a Poly is not a file reader nor a container. Let the Poly be a simple
coordinate pair (x and y) and another document class serialize the data to
and from the file. A third class might represent a Poly container which
encapsulates an array or vector of Polys.

This way, you'll be able to templatize the classes for reusability. Imagine
a document that can serialize any record type or a container that can hold /
store any data type.

void Read(const char[]) // name of the datafile
{
char out[sizes];
while(!infile.eof())
{
infile.getline(out,sizes);
strcpy(out2,out);
}
}

Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: (e-mail address removed) (Lind) - Find messages by this author
Date: 13 Apr 2005 06:32:15 -0700
Local: Wed,Apr 13 2005 6:32 am
Subject: how to read it out using c++
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

8->line 1
30 150
150 30
150 150
30 30
60 120
120 120
120 60
60 60
0 2 1 -3 4 7 6 -5->line2


this is what I have in my poly.dat
I'm quite confused on how to seperate it
I want to read my line1 data as an integer and line2 data should be
put into array which will be used for later code
and the rest line into multidimensional array because each line
represents an (x,y) of a point.


Can anyone figure this out?

What you are trying to do is standard practice. That's assuming you've
bothered reading up on std::strings, std::ifstreams, std::vector and the
like. Why not build a Document class that parses the document for you?
Instead of a vector of ints, you could write a class to hold an x and y
coordinate.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;

int main(int argc, char* argv[])
{
// A std::istream opens the specified file:

const string s_file = "poly.dat";
std::ifstream ifs;
ifs.open(s_file.c_str(), std::ios::in);
if(!ifs)
{
// error check or throw
std::cout << "error: can't open file.";
return -1;
}

// then read the first line which represents a record count
// (using std::getline).
// load the record count into n_count.

string s_input;
int n_count;
std::getline(ifs, s_input, '\n');
n_count = atoi(s_input.c_str());

// Now you have the record count

std::vector<int> vn; // container
string s; // temp string
int n_x; // left value
int n_y; // right value
int n_seperator; // space locator for parsing
int n_length; // end locator for parsing

for (int i = 0; i < n_count; i++)
{
std::getline(ifs, s_input, '\n'); // collect data string

// locate the space between numbers and end of data
n_seperator = s_input.find_first_of(' ');
n_length = s_input.length();

// parse s_input, load vector
s = s_input.substr(0, n_seperator);
n_x = atoi( s.c_str() );
vn.push_back(n_x);
s = s_input.substr(n_seperator + 1, n_length);
n_y = atoi( s.c_str() );
vn.push_back(n_y);
}

// display
for (i = 0; i < n_count; i++)
{
std::cout << "x[" << i << "]\t";
std::cout << vn[i * 2];

std::cout << "\ty[" << i << "]\t";
std::cout << vn[i * 2 + 1];
std::cout << "\n";
}

return 0;
} // main
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top