Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need help with my conversion utility...

Reply
Thread Tools

Need help with my conversion utility...

 
 
BobR
Guest
Posts: n/a
 
      10-15-2006

Martin Jørgensen wrote in message
<4531cee5$0$861$(E-Mail Removed)>.. .

/* """

/************************************************** ***/
void convert_data( unsigned int &linenumber,
const unsigned int &number_of_lines,
const ifstream &infile,
const string &cur_ofilename){

""" */

Just thought I'd throw in a little tip here. I noticed you used the arg
'ifstream &infile'.
If you change that to 'istream &' (toss the 'f'), you can pass different
streams to it:

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

void MyFunc( std::istream &in ){
std::string temp;
in >> temp; // or, std::getline( in, temp );
std::cout << temp <<std::endl;
return;
}

int main(){

// let user input something
MyFunc( std::cin ); // [1]

// use a 'set' stringstream for testing
std::istringstream MyTest( "I am what I am x 25." );
MyFunc( MyTest );

// send it a file to read
std::ifstream MyFile( "somefile.txt" );
MyFunc( MyFile );

return 0;
} // main()


[1] - I run in a GUI (wxWidgets), so, I'm not *positive* about this use. I
have tested 'std::cout' on an 'ostream&', which worked (output to dbg
debugger in IDE environ.).


Also, you mentioned you wanted to learn more about 'try/catch'. Besides many
good books, a good, free, download is Bruce Eckel's 'Thinking in C++' (vol 1
& 2). Volume 2 has a chapter on 'exceptions'. It should be enough to get you
going.

Get "Thinking in C++", 2nd ed. Volume 1 & 2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

Enjoy.
--
Bob R
POVrookie


 
Reply With Quote
 
 
 
 
Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
Guest
Posts: n/a
 
      10-16-2006
BobR wrote:

-snip-
> ??? Try removing the 'const' on the ifstream reference, and see what
> happens.


Thanks.

>> string read_line; // <<<<<<<<<<<-------- ERROR
>> infile.getline( read_line ); // <<<<<<<<<<<-------- ERROR

>
> Keep in mind that 'getline' reads the WHOLE line up to the next delimiter
> (which defaults to a newline '\n'), but does not read the newline
> (newline can be one char or two chars or ? depending on OS).


Ok, damn...

>> infile >> read_value;

>
> So, you could be trying to put "\n" into a type double var.


Damn... Ok, nobody said it was easy.

> Here is a little test I put together. Maybe it will give you an idea:

-snip-
> char at 15 is ., a alpha
> char at 16 is 7, a digit Inum=7 Dnum=7e-005
> char at 17 is e, a alpha
> char at 18 is -, a alpha
> char at 19 is 5, a digit Inum=5 Dnum=5
> _______ isdigit test _______Done
> */
> Your output may be different, I don't know what I had 'precision()' set
> to.


Yep, thanks...

-snip-
>
> Style police here - putting all your declarations at the top of a block is
> kinda 'C' like. In 'C++', we like to put them as close to usage as is
> reasonable.


Ok, thanks... I'm also more familiar with C, but I think C++ is really great
to learn as there's a lot of C++ programs out there...

> <snip>
> /************************************************** *****
> * scan the first input file for possible data types *
> ************************************************** *****/
> // So, move this to here:
> string output_data_type; /* part of output-filename */
> // ...it's first use is in the next line.
>
>> set_file_names(0, cur_ifilename, cur_ofilename, output_data_type);

> <snip>


Done. The line (and many others) was moved.

> // linenumber = 0; /* debug */
> // Move this to here: (etc.)
> unsigned int linenumber(0); /* current line */
>
>> while( getline( infile, read_line) ){

> <snip>
>> } // while(getline)

>
>> infile.close();

> // Hey, you ARE smart! I was about to point that out to you.
>
>
> Give those things some thought and come back with questions and/or let me
> know if you "got 'er done".


Finished!!! Hooray

Damn... That took much longer time than I had feared/expected... Most of my
weekend + friday went on this project, but it looks like it works under
Linux so it's nice to see that something pays off in the end and can be
used to something

I'm really glad for the help.... Sometimes I was just really stuck, but also
with the help of google and my 2 c++ books, it looks like I finally made
it...

> BTW, 'unsigned int' is usually typedefed to 'size_t', so you could use
> that (may need 'std::size_t' on newest implementations). I find it easier
> to type, but it's your choice.


Ok, thanks. I didn't knew that. It's just a habit I got from my bs.c.
C-coding project...

It looks like I'm done for now... I better stay in this group for a while
and see what else I can learn here. I appreciate your help. Now, I gotta
look at your Filer.h and TestFiler.cpp program/files... I'll be back once I
tried it more, in a few hours...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

 
Reply With Quote
 
 
 
 
Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
Guest
Posts: n/a
 
      10-16-2006
BobR wrote:

-snip-
> Just thought I'd throw in a little tip here. I noticed you used the arg
> 'ifstream &infile'.
> If you change that to 'istream &' (toss the 'f'), you can pass different
> streams to it:


Ok, got it...

> #include <iostream>
> #include <fstream>
> #include <string>
> #include <sstream>
>
> void MyFunc( std::istream &in ){
> std::string temp;
> in >> temp; // or, std::getline( in, temp );
> std::cout << temp <<std::endl;
> return;
> }
>
> int main(){
>
> // let user input something
> MyFunc( std::cin ); // [1]
>
> // use a 'set' stringstream for testing
> std::istringstream MyTest( "I am what I am x 25." );
> MyFunc( MyTest );
>
> // send it a file to read
> std::ifstream MyFile( "somefile.txt" );
> MyFunc( MyFile );
>
> return 0;
> } // main()
>
>
> [1] - I run in a GUI (wxWidgets), so, I'm not *positive* about this use. I
> have tested 'std::cout' on an 'ostream&', which worked (output to dbg
> debugger in IDE environ.).
>
>
> Also, you mentioned you wanted to learn more about 'try/catch'. Besides
> many good books, a good, free, download is Bruce Eckel's 'Thinking in C++'
> (vol 1 & 2). Volume 2 has a chapter on 'exceptions'. It should be enough
> to get you going.
>
> Get "Thinking in C++", 2nd ed. Volume 1 & 2 by Bruce Eckel
> (available for free here. You can buy it in hardcopy too.):
> http://www.mindview.net/Books/TICPP/...ngInCPP2e.html


Yep, I got the download book. The only thing holding me back is the lack of
time to do so much about it... I got a lot of books I would like to read,
if I had more time. I'm also trying to get through "Accelerated C++", but
it's going really slow... This week I've got vacation, so that also
explains why in the first place I tried to program this conversion utility
in C++... Normally I would have plenty of stuff to read or do (not much
related to programming)...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

 
Reply With Quote
 
Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
Guest
Posts: n/a
 
      10-16-2006
BobR wrote:

> I like to give credit where it's due. I forgot to mention that the heart
> of 'Filer.h' came from Andrew Koenig who said he learned it from Walter
> Brown:
>
> ifstream file("levels\\level1.txt");
> for ( string line; getline( file, line ); ) {
> // whatever
> }
>
> I thought it was such a slick little piece of code that I have replaced
> almost all of my 'while(readfile)' loops with it.
> Of course, you pick the proper tool for the job, not make the job fit the
> tool. <G>


Ok, now I understand the code. It basically loads everything into memory in
a vector<string>, so it can access any given line and insert lines/do
vector manipulations... But it's a bit complicated. I'm not that used to
all this inheritance and the like (although I wish I were), but it looks
nice, I agree...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-16-2006

Martin Jørgensen wrote in message
<4532fdfe$0$901$(E-Mail Removed)>.. .

>.... I'm not that used to all this inheritance and the like (although I wish

I were).....

You can write some nice C++ programs without ever knowing inheritance, but...

" Wow, if I had a sledge hammer instead of this machinists ballpeen hammer, I
could have knocked down that brick wall in one day instead of eight
months!!".

The more tools you have, the easier the job.

I guess you know now that you can not just sit down at your desk and declare,
"I'm going to learn C++ today!". I almost got discouraged when I read in an
NG an 'major mentor' said something like "there is no such thing as an C++
expert" (his meaning - we are all constantly learning the language). Don't
chase the rainbow looking for the pot of gold, just pick up the diamonds
along the way.

So, read when you can - books, this NG (and other C++/programming NGs
(alt.comp.lang.learn.c-c++ is another good one)), FAQs.
And code, code, and code some more! <G>

BTW - meant to ask you if you run (GNU/Linux) in a GUI. If you want an
*simple* IDE, you might take a look at:
MinGWStudio http://www.parinyasoft.com/
[ there is a Linux version, a little hard to find the link on their page, but
it's there.]
If you want full-blown, more bells-n-whistles than you need, there's
'Kdevelop'.
--
Bob R
POVrookie
-----
Dev-C++ IDE: http://www.bloodshed.net/
MinGW (GNU compiler): http://www.mingw.org/
wxWidgets URL: http://www.wxwidgets.org
V IDE & V GUI: http://www.objectcentral.com/
Quincy IDE 2005 URL: http://pipou.net/down/Quincy2005Project.zip
POVray: http://www.povray.org/
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Get "Thinking in C++", 2nd ed. Volume 1 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf


 
Reply With Quote
 
Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
Guest
Posts: n/a
 
      10-17-2006
BobR wrote:

>
> Martin Jørgensen wrote in message
> <4532fdfe$0$901$(E-Mail Removed)>.. .
>
>>.... I'm not that used to all this inheritance and the like (although I
>>wish

> I were).....
>
> You can write some nice C++ programs without ever knowing inheritance,
> but...
>
> " Wow, if I had a sledge hammer instead of this machinists ballpeen
> hammer, I could have knocked down that brick wall in one day instead of
> eight months!!".
>
> The more tools you have, the easier the job.


Agreed...

> I guess you know now that you can not just sit down at your desk and
> declare, "I'm going to learn C++ today!". I almost got discouraged when I
> read in an NG an 'major mentor' said something like "there is no such
> thing as an C++ expert" (his meaning - we are all constantly learning the
> language). Don't chase the rainbow looking for the pot of gold, just pick
> up the diamonds along the way.


Yep.

> So, read when you can - books, this NG (and other C++/programming NGs
> (alt.comp.lang.learn.c-c++ is another good one)), FAQs.
> And code, code, and code some more! <G>


Yep, coding is important...

> BTW - meant to ask you if you run (GNU/Linux) in a GUI. If you want an
> *simple* IDE, you might take a look at:
> MinGWStudio http://www.parinyasoft.com/
> [ there is a Linux version, a little hard to find the link on their page,
> [ but
> it's there.]


Ok, I just downloaded it but haven't really tried it yet... Until now I've
managed to do everything with emacs and the text console.

> If you want full-blown, more bells-n-whistles than you need, there's
> 'Kdevelop'.


Yep, I've tried it a bit... But I can't get the debugger to work from inside
it... I also just tried code:blocks and Code Forge... Also in code Forge I
couldn't get the debugger to work... It looks nice though...

> Dev-C++ IDE: http://www.bloodshed.net/
> MinGW (GNU compiler): http://www.mingw.org/
> wxWidgets URL: http://www.wxwidgets.org
> V IDE & V GUI: http://www.objectcentral.com/
> Quincy IDE 2005 URL: http://pipou.net/down/Quincy2005Project.zip
> POVray: http://www.povray.org/
> alt.comp.lang.learn.c-c++ faq:
> http://www.comeaucomputing.com/learn/faq/
> Get "Thinking in C++", 2nd ed. Volume 1 by Bruce Eckel
> (available for free here. You can buy it in hardcopy too.):
> http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
> Alf P. Steinbach's "Pointers" document:
> http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf


Nice list... I'll check it out...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-18-2006

Martin Jørgensen wrote in message
<4532f1be$0$921$(E-Mail Removed)>.. .
BobR wrote:

/* """
-snip-
>> string read_line; // <<<<<<<<<<<-------- ERROR
>> infile.getline( read_line ); // <<<<<<<<<<<-------- ERROR

>
> Keep in mind that 'getline' reads the WHOLE line up to the next delimiter
> (which defaults to a newline '\n'), but does not read the newline
> (newline can be one char or two chars or ? depending on OS).


Ok, damn...

>> infile >> read_value;

>
> So, you could be trying to put "\n" into a type double var.


Damn... Ok, nobody said it was easy.

""" */

Not saying that *is* your problem ( only that it *could be*).
If you do find yourself in that situation, there is a little thing
'*.ignore()' that can help 'skip over' stuff stuck in the buffer.

[ A little test snippet: ]
{
std::istringstream in("15 This is a string\n 09 12345");
int num1(0);
in >> num1;
in.ignore(1); // skip 1 char
std::string thestring;
std::getline( in, thestring, '\n');

// without the '.ignore()', the first char in 'thestring' would have been a
space (which I didn't want in this case).

int num2(0);
in >> num2;
std::cout <<"num1="<<num1<<" thestring="<<thestring
<<" num2="<<num2<<std::endl;
// out: num1=15 thestring=This is a string num2=9
}


/* """
> ... and/or let me know if you "got 'er done".


Finished!!! Hooray

Damn... That took much longer time than I had feared/expected... Most of my
weekend + friday went on this project, but it looks like it works under
Linux so it's nice to see that something pays off in the end and can be
used to something

""" */

If you only used standard C++ (seems like you did), it will also compile/run
for MinGW(on windows), on a Mac, or any compiler that is standard compliant
(or mostly compliant (as many are<G>) ).

For your gdb prob (in other post): try the gnu NGs - gnu.g++.help,
gnu.gdb.bug, gnu.gcc.help, etc. They might have some ideas. Be sure to tell
them what version of GCC, gdb you are using.

--
Bob R
POVrookie


 
Reply With Quote
 
=?UTF-8?B?TWFydGluIErDuHJnZW5zZW4=?=
Guest
Posts: n/a
 
      10-18-2006
BobR wrote:
> Martin Jørgensen wrote in message
> <4532f1be$0$921$(E-Mail Removed)>.. .

-snip-

>>> infile >> read_value;

>>
>>So, you could be trying to put "\n" into a type double var.

>
>
> Damn... Ok, nobody said it was easy.
>
> """ */
>
> Not saying that *is* your problem ( only that it *could be*).


Hmm... I can't remember what the problem was, but actually I think it
was that I had to read a whole text-line more, before trying to use
"infile >> read_value"...

My current code (which works) seems just to have a getline( infile,
readline) + string comparison with readline.substr( 0, readline.find('
') ), just to make sure the program reads the data that is expected...
(if not: quit with error). And after that infile >> readvalue works...

> If you do find yourself in that situation, there is a little thing
> '*.ignore()' that can help 'skip over' stuff stuck in the buffer.
>
> [ A little test snippet: ]
> {
> std::istringstream in("15 This is a string\n 09 12345");
> int num1(0);
> in >> num1;
> in.ignore(1); // skip 1 char


Great... But I also wanted my program to compile on linux as well as on
windows, so in Windows I think it would have to skip 2 char's (0D+0A).
So that is a ugly problem... I remember I thought pretty long about what
to do, so the best thing would be to use a
"read-string-command/function" that also reads the newline character -
that would work in windows+linux. And my program seems to do so, so I
think getline( inefile, readline ) also reads the newline... Else it
must be the infile >> read_value, that ignores the newline?

-snip-
> If you only used standard C++ (seems like you did), it will also compile/run
> for MinGW(on windows), on a Mac, or any compiler that is standard compliant
> (or mostly compliant (as many are<G>) ).


Yep, it works great, at least it seems like it does so

> For your gdb prob (in other post): try the gnu NGs - gnu.g++.help,
> gnu.gdb.bug, gnu.gcc.help, etc. They might have some ideas. Be sure to tell
> them what version of GCC, gdb you are using.


Yep... I'll just fiddle a bit more around with it... I think the culprit
is that some IDE's depend on having a makefile or else they don't know
what to do...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-18-2006

Martin Jørgensen wrote in message
<453618b9$0$915$(E-Mail Removed)>.. .
>
>Great... But I also wanted my program to compile on linux as well as on
>windows, so in Windows I think it would have to skip 2 char's (0D+0A).
>So that is a ugly problem...


My wording was a little flakey. Rather than stuffing my other hoof in my
already stuffed mouth, I suggest you read the docs for:

std::getline() // <bits/basic_string.h> (GCC)
and
std::istream::getline() // <istream>

My docs on those are a little old ('99), and I don't want to compound the
problem by posting them here.
I think where I misled you is that '\n' is defined differently depending on
the OS being compiled for ( I was thinking more about the file/stream pointer
(*.tellg, *.tellp).).
'getline()' reads the '\n' (1 or 2 chars), but doesn't put it in the storage
(string).

Guess I'll let this go before I have to grow a third hoof. <G>
[ easy to see why I leave the 'homework' questions to the experts. ]
--
Bob R
POVrookie


 
Reply With Quote
 
=?UTF-8?B?TWFydGluIErDuHJnZW5zZW4=?=
Guest
Posts: n/a
 
      10-19-2006
BobR wrote:
> Martin Jørgensen wrote in message
> <453618b9$0$915$(E-Mail Removed)>.. .
>
>>Great... But I also wanted my program to compile on linux as well as on
>>windows, so in Windows I think it would have to skip 2 char's (0D+0A).
>>So that is a ugly problem...

>
>
> My wording was a little flakey. Rather than stuffing my other hoof in my
> already stuffed mouth, I suggest you read the docs for:
>
> std::getline() // <bits/basic_string.h> (GCC)
> and
> std::istream::getline() // <istream>


Which docs? The c++ standard?

> My docs on those are a little old ('99), and I don't want to compound the
> problem by posting them here.
> I think where I misled you is that '\n' is defined differently depending on
> the OS being compiled for ( I was thinking more about the file/stream pointer
> (*.tellg, *.tellp).).


Yep, clear.

> 'getline()' reads the '\n' (1 or 2 chars), but doesn't put it in the storage
> (string).


Yep... I also found it in my "The C++ programming language" p.598,
where it says:

"The getline() function reads a line terminated by eol to its string,
expanding the string as needed to hold the line (§3.6). If no eol
argument is provided, a newline '\n' is used as the delimiter. The line
terminator is removed from the stream but not entered into the string.
Because a string expands to hold the input, there is no reason to leave
the terminator in the stream..."

> Guess I'll let this go before I have to grow a third hoof. <G>
> [ easy to see why I leave the 'homework' questions to the experts. ]


Well, I got help with those things that troubled me. LOL

Next, I'll see if I can mix a C-program with some C++ source code, which
I think is possible if I can tell MS visual studio (in linux: I think my
makefile?) that one of the source files is C++...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
help need in conversion problem senthil VHDL 1 02-26-2004 03:41 AM
numeric conversion error need your help al ASP .Net 2 01-14-2004 08:41 AM



Advertisments