Need help with my conversion utility...

  • Thread starter Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
  • Start date
B

BobR

Martin Jørgensen wrote in message
<[email protected]>...

/* """

/*****************************************************/
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/ThinkingInCPP2e.html

Enjoy.
 
M

Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=

BobR wrote:

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


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...
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.

<snip>

Done. The line (and many others) was moved.
// linenumber = 0; /* debug */
// Move this to here: (etc.)
unsigned int linenumber(0); /* current line */



// 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
 
M

Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=

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/ThinkingInCPP2e.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
 
M

Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=

BobR said:
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
 
B

BobR

Martin Jørgensen wrote in message
.... 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/ThinkingInCPP2e.html
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf
 
M

Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=

BobR said:
Martin Jørgensen wrote in message

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/ThinkingInCPP2e.html
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf

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


Best regards
Martin Jørgensen
 
B

BobR

Martin Jørgensen wrote in message
<[email protected]>...
BobR wrote:

/* """
-snip-
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...
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.
 
G

Guest

BobR said:
Martin Jørgensen wrote in message
<[email protected]>... -snip-



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
 
B

BobR

Martin Jørgensen wrote in message
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. ]
 
G

Guest

BobR said:
Martin Jørgensen wrote in message



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
 
B

BobR

Martin Jørgensen wrote in message
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++...

I think if you read/review the first three chapters of "Thinking in C++",
Volume 1, you'll reduce some problems in that task. (at least chap. 3).

It's interesting to take a 'C' program and compile it with g++, watch the
'type-safety' in action.
If you get no errors, then it was a pretty well designed 'C' program to begin
with.
[ I usually get *many* errors. (.... 'void *' to type 'double' ....).]

I think it's fun to take a single-file 'C' program and stuff the whole thing
(inc. global vars and 'main') into a 'class'. Then apply (std::) string,
fstream, vector, and it usually reduces code to 1/3. [ ain't perfect, the
executable usually explodes in size! <G>]


also see:
-----Original Message-----
From: Ivan Vecerina
Newsgroups: comp.lang.c++
Date: Thursday, October 19, 2006 1:18 AM
Subject: Re: How to link obj's from c-source with obj's from c++-source
 
G

Guest

BobR said:
Martin Jørgensen wrote in message



I think if you read/review the first three chapters of "Thinking in C++",
Volume 1, you'll reduce some problems in that task. (at least chap. 3).

It looks like a good book...
It's interesting to take a 'C' program and compile it with g++, watch the
'type-safety' in action.
If you get no errors, then it was a pretty well designed 'C' program to begin
with.
[ I usually get *many* errors. (.... 'void *' to type 'double' ....).]

Ok... It'll take some days before I try it out, as I've got plenty of
things to do (too much to read)...
I think it's fun to take a single-file 'C' program and stuff the whole thing
(inc. global vars and 'main') into a 'class'. Then apply (std::) string,
fstream, vector, and it usually reduces code to 1/3. [ ain't perfect, the
executable usually explodes in size! <G>]

I could imagine that :)
also see:
-----Original Message-----
From: Ivan Vecerina
Newsgroups: comp.lang.c++
Date: Thursday, October 19, 2006 1:18 AM
Subject: Re: How to link obj's from c-source with obj's from c++-source

Oh, thanks... Great... Looks like I'll have to try the following in a
few days:

#ifdef __cplusplus
extern "C" {
#endif


Best regards
Martin Jørgensen
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top