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

 
 
Martin =?UTF-8?B?SsO4cmdlbnNlbg==?=
Guest
Posts: n/a
 
      10-13-2006
Hi,

I've had a introductory C++ course in the spring and haven't programmed in
C++ for a couple of months now (but I have been programmed in C since
january). So I decided to do my conversion utility in C++, before I forget
everything. But it doesn't compile:



------------
> cat export_tex.C


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

using namespace std;


/*************************************************
* *
* This program takes data from some .vtk *
* input-files and exports them to .tex-format *
* *
*************************************************/


int main()
{
/* will contain LaTeX preamble etc */
string LaTeX_header = "test, bla.bla... \n\begin{document}\n";
string LaTeX_tail = "\end{document\n}";

string current_data_type;
char* valid_data_types[3] = {"Temperature", "Rho*cp", "Cell_energy"};
string read_line;

unsigned int filenumber; /* current data file number */
unsigned int number_of_lines; /* tells how many data lines follows */



/************************************************** **********
* conversion loop - continue as long as data-files exists *
************************************************** **********/

for(unsigned int i=0; i<999; i++)
{

ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */
if( infile )
{
infile >> read_line;

/* ??? if read_line == one of the "valid_data_types" ...
then begin conversion... Else: read a new line */
/* valid lines beginn with:
"SCALARS" + spc + |current_data_type| + spc */


/* test: cout read_line; */

/* tex_file[filenumber].tex */
ofstream outfile("tex_file", i, ".tex");


/**********************************
* do the actual conversion here *
**********************************/

outfile << LaTeX_header;

/*
for(unsigned int linenumber=1; linenumber<number_of_lines;
linenumber++)
{
infile >> something;
outfile << converted_something;
}
*/

outfile << LaTeX_tail;

}

if( ! infile ) /* can't open file? Then finish */
break;
}


cout << filenumber+1 << " number of .tex files written" << endl << endl;

return 0;
}


------------

As you see it's probably very much C-style and not very good C++-style, as I
did my bachelor project in C (and this conversion utility is a continuation
of my bs.c project).

First step: How can I make this compile? Suggestions are most welcome...


I get a lot of strange things when I try to compile... Such as:

/usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:43: error: expected
constructor, destructor, or type conversion before ‘namespace’
/usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error:
‘__gnu_debug_def’ is not a namespace-name
/usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error: expected
namespace-name before ‘;’ token
...... etc. etc...


Best regards
Martin Jørgensen

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

 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      10-13-2006
Hi,

Well this seems to come from the compiler itself, this means there is
something wrong with it. Just a wild guess; you might try to include the
'#include' s in a different order. If that doesn't work install another
version of g++.


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Martin Jrgensen" <(E-Mail Removed)> wrote in message
news:4530148b$0$822$(E-Mail Removed). ..
> Hi,
>
> I've had a introductory C++ course in the spring and haven't programmed in
> C++ for a couple of months now (but I have been programmed in C since
> january). So I decided to do my conversion utility in C++, before I forget
> everything. But it doesn't compile:
>
>
>
> ------------
>> cat export_tex.C

>
> #include <iostream>
> #include <fstream>
> #include <string>
>
> using namespace std;
>
>
> /*************************************************
> * *
> * This program takes data from some .vtk *
> * input-files and exports them to .tex-format *
> * *
> *************************************************/
>
>
> int main()
> {
> /* will contain LaTeX preamble etc */
> string LaTeX_header = "test, bla.bla... \n\begin{document}\n";
> string LaTeX_tail = "\end{document\n}";
>
> string current_data_type;
> char* valid_data_types[3] = {"Temperature", "Rho*cp", "Cell_energy"};
> string read_line;
>
> unsigned int filenumber; /* current data file number */
> unsigned int number_of_lines; /* tells how many data lines follows */
>
>
>
> /************************************************** **********
> * conversion loop - continue as long as data-files exists *
> ************************************************** **********/
>
> for(unsigned int i=0; i<999; i++)
> {
>
> ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */
> if( infile )
> {
> infile >> read_line;
>
> /* ??? if read_line == one of the "valid_data_types" ...
> then begin conversion... Else: read a new line */
> /* valid lines beginn with:
> "SCALARS" + spc + |current_data_type| + spc */
>
>
> /* test: cout read_line; */
>
> /* tex_file[filenumber].tex */
> ofstream outfile("tex_file", i, ".tex");
>
>
> /**********************************
> * do the actual conversion here *
> **********************************/
>
> outfile << LaTeX_header;
>
> /*
> for(unsigned int linenumber=1; linenumber<number_of_lines;
> linenumber++)
> {
> infile >> something;
> outfile << converted_something;
> }
> */
>
> outfile << LaTeX_tail;
>
> }
>
> if( ! infile ) /* can't open file? Then finish */
> break;
> }
>
>
> cout << filenumber+1 << " number of .tex files written" << endl << endl;
>
> return 0;
> }
>
>
> ------------
>
> As you see it's probably very much C-style and not very good C++-style, as
> I
> did my bachelor project in C (and this conversion utility is a
> continuation
> of my bs.c project).
>
> First step: How can I make this compile? Suggestions are most welcome...
>
>
> I get a lot of strange things when I try to compile... Such as:
>
> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:43: error:
> expected
> constructor, destructor, or type conversion before 'namespace'
> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error:
> '__gnu_debug_def' is not a namespace-name
> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error:
> expected
> namespace-name before ';' token
> ..... etc. etc...
>
>
> Best regards
> Martin Jrgensen
>
> --
> ---------------------------------------------------------------------------
> Home of Martin Jrgensen - http://www.martinjoergensen.dk
>



 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      10-13-2006


Hi,

Wait a minute, your file ends with .cc or not? Make sure you compile it with
g++ and not gcc.


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Moonlit" <news moonlit xs4all nl> wrote in message
news:45301cce$0$4528$(E-Mail Removed)4all.nl...
> Hi,
>
> Well this seems to come from the compiler itself, this means there is
> something wrong with it. Just a wild guess; you might try to include the
> '#include' s in a different order. If that doesn't work install another
> version of g++.
>
>
> Regards, Ron AF Greve
>
> http://moonlit.xs4all.nl
>
> "Martin Jrgensen" <(E-Mail Removed)> wrote in message
> news:4530148b$0$822$(E-Mail Removed). ..
>> Hi,
>>
>> I've had a introductory C++ course in the spring and haven't programmed
>> in
>> C++ for a couple of months now (but I have been programmed in C since
>> january). So I decided to do my conversion utility in C++, before I
>> forget
>> everything. But it doesn't compile:
>>
>>
>>
>> ------------
>>> cat export_tex.C

>>
>> #include <iostream>
>> #include <fstream>
>> #include <string>
>>
>> using namespace std;
>>
>>
>> /*************************************************
>> * *
>> * This program takes data from some .vtk *
>> * input-files and exports them to .tex-format *
>> * *
>> *************************************************/
>>
>>
>> int main()
>> {
>> /* will contain LaTeX preamble etc */
>> string LaTeX_header = "test, bla.bla... \n\begin{document}\n";
>> string LaTeX_tail = "\end{document\n}";
>>
>> string current_data_type;
>> char* valid_data_types[3] = {"Temperature", "Rho*cp", "Cell_energy"};
>> string read_line;
>>
>> unsigned int filenumber; /* current data file number */
>> unsigned int number_of_lines; /* tells how many data lines follows */
>>
>>
>>
>> /************************************************** **********
>> * conversion loop - continue as long as data-files exists *
>> ************************************************** **********/
>>
>> for(unsigned int i=0; i<999; i++)
>> {
>>
>> ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */
>> if( infile )
>> {
>> infile >> read_line;
>>
>> /* ??? if read_line == one of the "valid_data_types" ...
>> then begin conversion... Else: read a new line */
>> /* valid lines beginn with:
>> "SCALARS" + spc + |current_data_type| + spc */
>>
>>
>> /* test: cout read_line; */
>>
>> /* tex_file[filenumber].tex */
>> ofstream outfile("tex_file", i, ".tex");
>>
>>
>> /**********************************
>> * do the actual conversion here *
>> **********************************/
>>
>> outfile << LaTeX_header;
>>
>> /*
>> for(unsigned int linenumber=1; linenumber<number_of_lines;
>> linenumber++)
>> {
>> infile >> something;
>> outfile << converted_something;
>> }
>> */
>>
>> outfile << LaTeX_tail;
>>
>> }
>>
>> if( ! infile ) /* can't open file? Then finish */
>> break;
>> }
>>
>>
>> cout << filenumber+1 << " number of .tex files written" << endl << endl;
>>
>> return 0;
>> }
>>
>>
>> ------------
>>
>> As you see it's probably very much C-style and not very good C++-style,
>> as I
>> did my bachelor project in C (and this conversion utility is a
>> continuation
>> of my bs.c project).
>>
>> First step: How can I make this compile? Suggestions are most welcome...
>>
>>
>> I get a lot of strange things when I try to compile... Such as:
>>
>> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:43: error:
>> expected
>> constructor, destructor, or type conversion before 'namespace'
>> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error:
>> '__gnu_debug_def' is not a namespace-name
>> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:47: error:
>> expected
>> namespace-name before ';' token
>> ..... etc. etc...
>>
>>
>> Best regards
>> Martin Jrgensen
>>
>> --
>> ---------------------------------------------------------------------------
>> Home of Martin Jrgensen - http://www.martinjoergensen.dk
>>

>
>



 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-13-2006

> > cat export_tex.C


....

> First step: How can I make this compile? Suggestions are most welcome...


It's complaining about the word "using," which is C++ specific.

My guess - change the filename to .CC or .cc or .cpp or something like
that. At least a couple of compilers I've used have a heuristic that
checks filename and compiles in C mode or C++ mode depending. And be
sure you're using the C++ version of the compiler (g++ instead of gcc).

I tried compiling in VS, and it complains, but not about that.

Michael

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      10-13-2006

Martin Jrgensen wrote:
> Hi,
>
> I've had a introductory C++ course in the spring and haven't programmed in
> C++ for a couple of months now (but I have been programmed in C since
> january). So I decided to do my conversion utility in C++, before I forget
> everything. But it doesn't compile:
>
>
>
> ------------
> > cat export_tex.C

>
> #include <iostream>
> #include <fstream>
> #include <string>
>
> using namespace std;
>
>
> /*************************************************
> * *
> * This program takes data from some .vtk *
> * input-files and exports them to .tex-format *
> * *
> *************************************************/
>
>
> int main()
> {
> /* will contain LaTeX preamble etc */
> string LaTeX_header = "test, bla.bla... \n\begin{document}\n";
> string LaTeX_tail = "\end{document\n}";


Keep in mind that C++ uses "\" as an escape character, so the above
line probably doesn't do what you expect.


> string current_data_type;
> char* valid_data_types[3] = {"Temperature", "Rho*cp", "Cell_energy"};
> string read_line;
>
> unsigned int filenumber; /* current data file number */
> unsigned int number_of_lines; /* tells how many data lines follows */
>
>
>
> /************************************************** **********
> * conversion loop - continue as long as data-files exists *
> ************************************************** **********/
>
> for(unsigned int i=0; i<999; i++)
> {
>
> ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */


The above line does not use a valid constructor for ifstream.
ifstream's constructor is declared as follows:

explicit basic_ifstream(const char *filename, ios_base:penmode mode =
ios_base::in);

In other words, your three argument constructor isn't valid, AFAIK.


> if( infile )
> {
> infile >> read_line;
>
> /* ??? if read_line == one of the "valid_data_types" ...
> then begin conversion... Else: read a new line */
> /* valid lines beginn with:
> "SCALARS" + spc + |current_data_type| + spc */
>
>
> /* test: cout read_line; */
>
> /* tex_file[filenumber].tex */
> ofstream outfile("tex_file", i, ".tex");
>
>
> /**********************************
> * do the actual conversion here *
> **********************************/
>
> outfile << LaTeX_header;
>
> /*
> for(unsigned int linenumber=1; linenumber<number_of_lines;


You never initialized number_of_lines.

That's a start, anyway.

Best regards,

Tom

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      10-13-2006
Martin Jrgensen wrote:

>> cat export_tex.C


Is this C or C++? Sometimes compilers trigger on the filename suffix, so see
if that fixes it.

(However, your compiler's forum is the best place for subsequent questions
on how to drive it...)

> I get a lot of strange things when I try to compile... Such as:
>
> /usr/include/c++/4.1.0/i586-suse-linux/bits/c++config.h:43: error:
> expected
> constructor, destructor, or type conversion before 'namespace'


A 'config.h' is often the very first header file of any translation unit,
because (again, off-topically) many compilers rely on install scripts that
build config.h with their platform-specific details.

However, your compiler can't understand its first C++ line. Maybe the
compiler is in C mode, and then maybe the error message incorrectly
specifies C things.

Alternately, maybe your compiler is old and doesn't understand namespaces,
which are new. Try these:

$ which c++

That might show a path with a version other than 4.1

$ ls -l `which c++`

That might show a link to path with a version other than 4.1

$ c++ -v

And that might show version info other than 4.1.

And, naturally, you might be calling some wrapper other than c++, such as
g++. Check your makefile.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


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

-snip-
>> /* will contain LaTeX preamble etc */
>> string LaTeX_header = "test, bla.bla... \n\begin{document}\n";
>> string LaTeX_tail = "\end{document\n}";

>
> Keep in mind that C++ uses "\" as an escape character, so the above
> line probably doesn't do what you expect.


Thanks. I think \\ must be the correct thing then.

-snip-
>> for(unsigned int i=0; i<999; i++)
>> {
>>
>> ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */

>
> The above line does not use a valid constructor for ifstream.
> ifstream's constructor is declared as follows:
>
> explicit basic_ifstream(const char *filename, ios_base:penmode mode =
> ios_base::in);
>
> In other words, your three argument constructor isn't valid, AFAIK.


Damn... I wanted the loop to process these file names:

file000.vtk
file001.vtk
file002.vtk
file003.vtk
.....
.....

So that explains the "file" + the counter in the loop, "i", + extension. How
to do this, then? And my original suggestion also didn't take into account
that the numbers should always take up 3 characters. Here's my
C-implementation (this is how I make the output filenames in C):


--- for-loop --
if(*filecounter >999)
{
printf("Error! Too many results have been written.\n\n");
quit_program(__FILE__, __LINE__);
}

sprintf(fname, "file_%03d.vtk", *file_counter);
---


How to this in C++? - In an "elegant" way, ofcourse...

>> if( infile )
>> {
>> infile >> read_line;


Here I want to read a line... Not sure I did it correctly...

>> /* ??? if read_line == one of the "valid_data_types" ...
>> then begin conversion... Else: read a new line */


I must compare the beginning of each line with the word "SCALARS". Once I
find that word in the beginning of a line, I must see if it's one of the 3
char *valid_data_types[3].

-snip-
>> /*
>> for(unsigned int linenumber=1; linenumber<number_of_lines;

>
> You never initialized number_of_lines.
>
> That's a start, anyway.


You're right, thanks... The number of lines is something I'll have to
extract from inside the data-file and I haven't exactly figured out how to
do that yet...


I can now compile my program. I had a stupid "x" in the top of the file and
after I removed the ifstream infile("file", i, ".vtk") line (and also for
the ofstream) I got something usable:

Complete program follows:
------------------------

> cat output_to_latex.cpp

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

using namespace std;


/*************************************************
* *
* This program takes data from some .vtk *
* input-files and exports them to .tex-format *
* *
*************************************************/


int main()
{
/* will contain LaTeX preamble etc */
string LaTeX_header = "test, bla.bla... \n\\begin{document}\n";
string LaTeX_tail = "\\end{document}\n";

string current_data_type;
char* valid_data_types[3] = {"Temperature", "Rho*cp", "Cell_energy"};
string read_line;

unsigned int filenumber; /* current data file number */
unsigned int number_of_lines; /* tells how many data lines follows */



/************************************************** **********
* conversion loop - continue as long as data-files exists *
************************************************** **********/

for(unsigned int i=0; i<999; i++)
{

ifstream infile("file000.vtk");
// ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */

if( infile )
{
infile >> read_line;

/* ??? if read_line == one of the "valid_data_types" ...
then begin conversion... Else: read a new line */
/* valid lines beginn with:
"SCALARS" + spc + |current_data_type| + spc */


/* test: cout read_line; */
ofstream outfile("tex_file000.tex");
/* should be tex_file[filenumber].tex, something like:
ofstream outfile("tex_file", i, ".tex"); */


/**********************************
* do the actual conversion here *
**********************************/

outfile << LaTeX_header;

/*
for(unsigned int linenumber=1; linenumber<number_of_lines;
linenumber++)
{
infile >> something;
outfile << converted_something;
}
*/

outfile << LaTeX_tail;

}

if( ! infile ) /* can't open file? Then finish */
break;
}


cout << filenumber+1 << " number of .tex files written" << endl << endl;

return 0;
}

------------------------

Suggestions are welcome... I probably should make a file-class and then have
a smaller main-function with a loop that just increments the file-counter
and calls some function in that class... or something...

I still have a few problems to solve...


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-14-2006
Phlip wrote:

> Martin Jrgensen wrote:
>
>>> cat export_tex.C

>
> Is this C or C++? Sometimes compilers trigger on the filename suffix, so
> see if that fixes it.


I've read that .c is C and .C is C++ on unix... And it didn't make any
difference to use .cpp, but now I use .cpp even though it isn't necessary.



Best regards
Martin Jørgensen

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

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

Michael wrote in message
<(E-Mail Removed). com>...
>
>> > cat export_tex.C

>...
>
>> First step: How can I make this compile? Suggestions are most welcome...

>
>It's complaining about the word "using," which is C++ specific.
>
>My guess - change the filename to .CC or .cc or .cpp or something like
>that. At least a couple of compilers I've used have a heuristic that
>checks filename and compiles in C mode or C++ mode depending. And be
>sure you're using the C++ version of the compiler (g++ instead of gcc).


[ GCC Docs ]
For any given input file, the file name suffix determines what kind of
compilation is done:

file.c
C source code which must be preprocessed.
[ use gcc ]

file.cc
file.cp
file.cxx
file.cpp
file.c++
file.C
C++ source code which must be preprocessed. Note that in .cxx, the last
two
letters must both be literally x. Likewise, .C refers to a literal
capital C.
[ use g++ ]

--
Bob R
POVrookie


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

Martin Jørgensen wrote in message
<45302fc7$0$895$(E-Mail Removed)>.. .
Thomas Tutone wrote:

-snip-
>
>> Keep in mind that C++ uses "\" as an escape character, so the above
>> line probably doesn't do what you expect.


>Thanks. I think \\ must be the correct thing then.


Yup!

-snip-
>>> for(unsigned int i=0; i<999; i++){
>>> ifstream infile("file", i, ".vtk"); /* file[filenumber].vtk */

>>
>> The above line does not use a valid constructor for ifstream.
>> In other words, your three argument constructor isn't valid, AFAIK.


>Damn... I wanted the loop to process these file names:
>file000.vtk
>file001.vtk
>file002.vtk
>file003.vtk
>....

/* "
So that explains the "file" + the counter in the loop, "i", + extension. How
to do this, then?
And my original suggestion also didn't take into account
that the numbers should always take up 3 characters. Here's my
C-implementation (this is how I make the output filenames in C):

How to this in C++? - In an "elegant" way, ofcourse...
" */

One way is to use 'stringstream' (header <sstream>), then put it in a
std::string.

#include <string>
#include <sstream> // for std:stringstream
#include <iomanip> // for std::setfill(), std::setw()

int fnumb(5); // you will use the 'int' in your for loop.
std:stringstream Oss;
Oss << "file" <<std::setfill( '0' )<<std::setw( 3 )<< fnumb << ".vtk";
std::string Filename;
Filename = Oss.str();
// or just: std::string Filename( Oss.str() );

std::cout<<" Filename ="<<Filename<<std::endl;
// --- output ---
// Filename =file005.vtk

ifstream infile( Filename.c_str() ); /* file[filenumber].vtk */


>>> if( infile ){
>>> infile >> read_line;

>Here I want to read a line... Not sure I did it correctly...


std::string read_line;

std::getline( infile, read_line );

>>> /* ??? if read_line == one of the "valid_data_types" ...
>>> then begin conversion... Else: read a new line */


>I must compare the beginning of each line with the word "SCALARS". Once I
>find that word in the beginning of a line, I must see if it's one of the 3
>char *valid_data_types[3].


std::string FindThis( "SCALARS" );
if( read_line.find( FindThis ) != std::string::npos ){
/* "SCALARS" is in read_line */
std::string FindThis1( valid_data_types[0] ); // may need 'adjusting'
(*)
if( read_line.find( FindThis1 ) != std::string::npos ){
/* valid_data_types[0] is in read_line */
}
}



Read-up on std::string and std::streams, then you'll have much power at your
command! <G>

Hope that helps. This ain't homework is it?
--
Bob R
POVrookie


 
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