473,804 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.len gth()-1; i<j; i++) {
if( Destination[i] == ' ' || Destination[i] == '\t' ) {
Destination.era se( 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<<"(StringT oInt): Source:"<<Sourc e<<" Dest: "<<*Dest<<e ndl;
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_H PP_
#define DXFFILEREADER_H PP_

#include <string>
#include <fstream>
#include <iostream>
#include "DXFToken.h pp"

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.len gth();
for( int i = 0; i<j; i++) {
if( Destination[i] == ' ' || Destination[i] == '\t' ) {
Destination.era se( i, 1 );
j--;
}
}
cout<<"StripWhi teSpace:"<<Dest ination<<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.lengt h()<<endl;
cout<<"last char: "<<Source[1]<<endl;
return false;
}
}

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

cout<<"(StringT oInt): Source:"<<Sourc e<<" Dest: "<<*Dest<<e ndl;
return true;
}

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

getline( dxfFileStream, Destination, '\n');
StripWhiteSpace ( Destination );
cout<<"GetDXFLi ne: "<<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\nestor ovd\workspace\D XFReader\Debug> "C:
\Documents and Settings\nestor ovd\workspace\D XFReader\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 1691
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::istringstr eam iss(s);
if (!(iss >t))
throw std::string("Ca n't convert '") + s +
"' to '" + typeid(t).name( ) + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::stri ng& 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::istringstr eam iss(s);
if (!(iss >t))
throw std::string("Ca n't convert '") + s +
"' to '" + typeid(t).name( ) + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::stri ng& 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.era se( 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
2498
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 ^I^M Some text with occasional highlighting. Some text with occasional^M highlighting. Some text with occasional <high>highlighting</high>.^M Some text with occasional highlighting. Some <high>text</high> with^M occasional highlighting.</para>^M
2
6969
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
15021
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 spaces(if it more than once between two words). Is there any function we have in C which will find out the tabs and white spaces and returns the text in the follwong way -
5
3220
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
3371
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 properties for all the controls to "Dynamic" and still I can't get rid of the white spaces between controls. It's as if there are bunch of <br> tags separating the controls. For example,the "HTML" look similar to this.
6
2690
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
1805
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 http://rep.oio.dk/Microsoft.com/officeschemas/welcome.htm) to a reasonably clean (X)HTML format. (The reason being that, combined with some PHP scripting it should be possible to store the embedded images, which is pretty neat).
12
5583
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
1850
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
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.