473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to read it out?

Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: piss...@gmail.com (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?

Jul 23 '05 #1
4 1833

<pi*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: piss...@gmail.com (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
Jul 23 '05 #2
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[i],out);
}
}
codigo wrote:
<pi*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: piss...@gmail.com (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


Jul 23 '05 #3
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[i],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.

Jul 23 '05 #4

"lind" <pi*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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[i],out);
}
}
codigo wrote:
<pi*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Lind Apr 13, 6:32 am show options

Newsgroups: comp.lang.c
From: piss...@gmail.com (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

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
4
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.