473,387 Members | 1,876 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,387 software developers and data experts.

Problem with stripping white spaces and references!?

Hi All,

I have the following problem:
I read lines from DXF file ( AutoCAD format file ). Then I need to
remove white spaces from lines to continue working on data i.e.
converting from string to int and so on. My StripWhiteSpace function
works in test program:

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

using namespace std;

void StripWhiteSpace( string& Destination )
{
for( int i = 0, j = Destination.length()-1; i<j; i++) {
if( Destination[i] == ' ' || Destination[i] == '\t' ) {
Destination.erase( i, 1 );
j--;
}
}
}

bool StringToInt( string& Source, int *Dest )
{
cout<<"Calling StringToInt with:"<<Source<<endl;

unsigned int i = 0;
bool isNegative = false;

*Dest = 0;

if( Source[0] == '-' ) {
isNegative = true;
i = 1;
}

for( ; i<Source.length(); i++ ) {
if( Source[i] >= '0' && Source[i] <= '9' ) {
if( ( i == 0 && isNegative == false ) || ( i == 1 && isNegative ==
true ) ){
*Dest = Source[i] - '0';
}else{
*Dest = (*Dest)*10 + Source[i] - '0';
}
}else{
return false;
}
}

if( isNegative )
*Dest = (*Dest)*-1;

cout<<"(StringToInt): Source:"<<Source<<" Dest: "<<*Dest<<endl;
return true;
}

int main()
{
ifstream fin("test.dxf");
string a;
while( !fin.eof() ) {
fin>>a;
StripWhiteSpace( a );
cout<<a<<endl;
}

return 0;
}

but doesn't work in my real program ( part of it below )

#ifndef DXFFILEREADER_HPP_
#define DXFFILEREADER_HPP_

#include <string>
#include <fstream>
#include <iostream>
#include "DXFToken.hpp"

using namespace std;

class DXFFileReader
{
private:
ifstream dxfFileStream;
private:
void StripWhiteSpace( string& Destination );
bool StringToInt( string& Source, int *Dest );
bool GetDXFLine( string& Destination );
public:
DXFFileReader( string dxfFileToRead );
bool IsOpened();
bool GetDXFToken( DXFToken *dxfToken );
};

#endif /*DXFFILEREADER_HPP_*/

void DXFFileReader::StripWhiteSpace( string& Destination )
{
int j = Destination.length();
for( int i = 0; i<j; i++) {
if( Destination[i] == ' ' || Destination[i] == '\t' ) {
Destination.erase( i, 1 );
j--;
}
}
cout<<"StripWhiteSpace:"<<Destination<<endl;
}

bool DXFFileReader::StringToInt( string& Source, int *Dest )
{
cout<<"Calling StringToInt with: "<<Source<<endl;
unsigned int i = 0;
bool isNegative = false;

*Dest = 0;

if( Source[0] == '-' ) {
isNegative = true;
i = 1;
}

for( ; i<Source.length(); i++ ) {
if( Source[i] >= '0' && Source[i] <= '9' ) {
if( ( i == 0 && isNegative == false ) || ( i == 1 && isNegative ==
true ) ){
*Dest = Source[i] - '0';
}else{
*Dest = (*Dest)*10 + Source[i] - '0';
}
}else{
cout<<"return false: "<<Source[i]<<" string legth:
"<<Source.length()<<endl;
cout<<"last char: "<<Source[1]<<endl;
return false;
}
}

if( isNegative )
*Dest = (*Dest)*-1;

cout<<"(StringToInt): Source:"<<Source<<" Dest: "<<*Dest<<endl;
return true;
}

bool DXFFileReader::GetDXFLine( string& Destination )
{
if( !dxfFileStream.is_open() || dxfFileStream.eof() )
return false;

getline( dxfFileStream, Destination, '\n');
StripWhiteSpace( Destination );
cout<<"GetDXFLine: "<<Destination<<endl;

return true;
}

bool DXFFileReader::GetDXFToken( DXFToken *dxfToken )
{
string CodeLine, ValueLine;
int Code = 0;

if( GetDXFLine( CodeLine ) && GetDXFLine( ValueLine ) ) {
if( !StringToInt( CodeLine, &Code ) ) {
return false;
}else{
if( Code == 999 ) {
//This is comment :)
} else if( (Code >=0 && Code <= 9) ||
(Code >=300 && Code <= 309) ||
(Code >=1000 && Code <= 1009) ||
Code == 100 ||
Code == 102 ) {
dxfToken->AssignToken( Code, ValueLine );
}else if( (Code >=60 && Code <= 79) ||
(Code >=90 && Code <= 99) ||
(Code >=170 && Code <= 175) ||
(Code >=1060 && Code <= 1071) ||
(Code >=280 && Code <= 289) ) {

int Value = 0;
if( !StringToInt( ValueLine, &Value ) )
return false;

dxfToken->AssignToken( Code, Value );
}else{
return false;
}
}
}else{
return false;
}
return true;
}

the output is as follows:

C:\Documents and Settings\nestorovd\workspace\DXFReader\Debug>"C:
\Documents and Settings\nestorovd\workspace\DXFReader\Debug
\DXFReader.exe"
success openning file
StripWhiteSpace: 0
GetDXFLine: 0
StripWhiteSpace:SECTION
GetDXFLine: SECTION
Calling StringToInt with: 0
return false: string legth: 2
last char: 0

What I am doing wrong? Please help!

Apr 3 '07 #1
2 1659
On 3 Apr, 10:21, delyan.nesto...@gmail.com wrote:
Hi All,

I have the following problem:
I read lines from DXF file ( AutoCAD format file ). Then I need to
remove white spaces from lines to continue working on data i.e.
converting from string to int and so on. My StripWhiteSpace function
works in test program:
Can't help you with your code but I can give you something else that
might be usedful, the following code can be used to convert strings to
a number of types (all the native builtin) even if there are spaces:

#include <iostream>
#include <sstream>
#include <string>

/*
* Converts a string to some other type. The type converted to must
* have a std::ostream& operator>>(std::ostream&, T) method declared.
* Works for all built-in types.
*/
template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstream iss(s);
if (!(iss >t))
throw std::string("Can't convert '") + s +
"' to '" + typeid(t).name() + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::string& s) {
std::cout << s;
}
return 0;
}

It's my own adoption of the code from the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-39.2

--
Erik Wikström

Apr 3 '07 #2
Erik Wikstrom íàïèñà:
On 3 Apr, 10:21, delyan.nesto...@gmail.com wrote:
Hi All,

I have the following problem:
I read lines from DXF file ( AutoCAD format file ). Then I need to
remove white spaces from lines to continue working on data i.e.
converting from string to int and so on. My StripWhiteSpace function
works in test program:

Can't help you with your code but I can give you something else that
might be usedful, the following code can be used to convert strings to
a number of types (all the native builtin) even if there are spaces:

#include <iostream>
#include <sstream>
#include <string>

/*
* Converts a string to some other type. The type converted to must
* have a std::ostream& operator>>(std::ostream&, T) method declared.
* Works for all built-in types.
*/
template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstream iss(s);
if (!(iss >t))
throw std::string("Can't convert '") + s +
"' to '" + typeid(t).name() + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::string& s) {
std::cout << s;
}
return 0;
}

It's my own adoption of the code from the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-39.2

--
Erik Wikstrom
Hi again,

Thanks Erik this was usefull for me!

And I found my STUPID error:

if( Destination[i] == ' ' || Destination[i] == '\t' ) {
Destination.erase( i, 1 );
j--; i--; <----HERE this "i--" missed
hahaha, horror! Today my head is full with s***s
}

Apr 3 '07 #3

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

Similar topics

1
by: Andy Jefferies | last post by:
I'm having problems stripping out the whitespace at the beginning of a particular element. In the XML snippet I've highlighted tabs and returns as ^I and ^M respectively: <para> ^I ^I ...
2
by: Sergio del Amo Caballero | last post by:
Hi, I have a structure like this in my xhtml 1.0 file. <body> <div> <a> <img ... id="f1" /> <span>....</span><br/> <span>....</span> </a> ---> <div>
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
5
by: Thor W Hammer | last post by:
Smart algorithm for normalizing strings anyone? Example: If the original string looks like this: "the brown fox " I want it end up like this: "the brown fox"
3
by: Prince | last post by:
I have some <RequiredFieldValidator> on my page and everything works fine except that there are lots of white spaces between the web server controls that are being validated. I've set the Display...
6
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
4
by: dwergkees | last post by:
Hi, Got a litte problem here. I'm trying to create a XSLT file that will do a transformation from WordML format (MS Word XML format, see...
12
by: Vadim Guchenko | last post by:
Hello. I'm using the following code: <html> <head> <style type="text/css"> pre {display: inline;} </style> </head>
8
by: Dennis | last post by:
Why does this page... http://kowallekfamily.com/genealogy/reports/desc-1766.xml ....display fine in Firefox 2.0.0.14 but gets an error in IE6? In IE6 I get the message... TIA,
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.