By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

Need help with my conversion utility...

P: n/a
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

Oct 13 '06 #1
Share this Question
Share on Google+
31 Replies


P: n/a
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" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
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

Oct 13 '06 #2

P: n/a


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 nlwrote in message
news:45**********************@news.xs4all.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" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
>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


Oct 13 '06 #3

P: n/a
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

Oct 13 '06 #4

P: n/a

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::openmode 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

Oct 13 '06 #5

P: n/a
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!!!
Oct 13 '06 #6

P: n/a
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::openmode 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

Oct 14 '06 #7

P: n/a
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

Oct 14 '06 #8

P: n/a

Michael wrote in message
<11*********************@k70g2000cwa.googlegroups. 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
Oct 14 '06 #9

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
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::ostringstream
#include <iomanip // for std::setfill(), std::setw()

int fnumb(5); // you will use the 'int' in your for loop.
std::ostringstream 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
Oct 14 '06 #10

P: n/a
BobR wrote:

-snip-
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?
Nope. No teacher gives me any homework that extends my bs.c. project because
I'm not a software/electrical engineer or something like that. I'm a
mechanical engineer (studying to get my masters degree) so the only thing
the teachers want is Matlab programming. This is just so I don't forget all
I learned about c++.

I took your suggestions and implemented some of it and rewrote everything...
It's much more C++'ish, but I can't compile it...

Just copy/paste, compile and tell me your suggestions.
----- unfinished code begins ----
#include <iostream>
#include <fstream>
#include <string>
#include <sstream/* for std::ostringstream */
#include <iomanip/* for std::setfill(), std::setw() */
#include <vector>
#include <ctype.h/* for isspace */

using namespace std;
std::string trim( std::string const& s )
{
// Create string from first non-whitespace to last non-whitespace

return std::string ( std::find_if( s.begin(), s.end(), std::not
(std::ptr_fun(&isspace) ) ),
std::find_if( s.rbegin(), s.rend(), std::not
(std::ptr_fun(&isspace) ) ).base() );

}

/************************************************** ***/

void convert_data(unsigned int linenumber,
const string &cur_ifilename, const string &cur_ofilename)
{

string LaTeX_header = "test, bla.bla... \n\\begin{document}\n";
string LaTeX_tail = "\\end{document}\n";

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

// ofstream outfile( cur_ofilename::c_str() ); <<<< WRONG???

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

/************************************************** ***/

void set_file_names(unsigned int filenumber, string &cur_ifilename,
string &cur_ofilename)
{

/*************************
* Creating file names *
*************************/

ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();
filename_generated << "tex_file" << setfill('0') <<
setw(3) << filenumber << ".tex";

/* WRONG!!! = file000.vtktex_file000.tex ??? */
cur_ofilename = filename_generated.str();

cout << "Input filename = " << cur_ifilename << endl;
cout << "Output filename = " << cur_ofilename << endl;
}

/************************************************** ***/

int main()
{
string current_data_type;
vector<stringpossible_data_types;
int data_type;
string read_line;

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

string cur_ifilename; /* input */
string cur_ofilename; /* output */

/************************************************** *****
* scan the first input file for possible data types *
************************************************** *****/

set_file_names(0, cur_ifilename, cur_ofilename);

ifstream infile( cur_ifilename.c_str() );
while( infile )
{
getline( infile, read_line);

if( read_line.find("SCALARS") != string::npos )
{
/* "SCALARS" was found in read_line */
cout << read_line << endl;

/* "SCALARS" + spc + |current_data_type| + spc */
current_data_type = trim( read_line );
cout << current_data_type << endl;
possible_data_types.push_back(current_data_type);
}
}
/*********************************************
* ask which data type should be converted *
********************************************/
data_type = 0;
for( vector<string::const_iterator it = possible_data_types.begin();
it != possible_data_types.end(); ++it)
{
cout << data_type << ": " << possible_data_types(*it) << endl;
}
cout << endl << "Which datatype (number) should be converted ? ";
cin >data_type;

current_data_type = possible_data_types[data_type];

/*******************************
* convert data in all files *
*******************************/

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

set_file_names(filenumber, cur_ifilename, cur_ofilename);

linenumber = 0;
ifstream infile( cur_ifilename.c_str() );

while( infile )
{

linenumber++; /* counter for debugging */
getline( infile, read_line);

if( read_line.find("SCALARS") != string::npos )
{
/* "SCALARS" was found in read_line */
cout << read_line << endl; /* debug */

/* if( read_line contains "current_data_type" - convert)
/* "SCALARS" + spc + |current_data_type| + spc */

convert_data( linenumber, cur_ifilename, cur_ofilename);

cout << "Program not finished. Come back later" << endl;
return 1;
}
cout << "Program not finished. Come back later" << endl;
return 1;
}
}
cout << filenumber+1 << " number of .tex files written" << endl << endl;
return 0;
}

----- unfinished code ends ----
Best regards
Martin Jørgensen

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

Oct 14 '06 #11

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
BobR wrote:

-snip-
>This ain't homework is it?
>Nope.
Good. We don't like to do the homework for someone, but rather, guide a
student.
I like to play with code, so I usually leave the homework to the guys higher
on the ladder than me. <G>

>I took your suggestions and implemented some of it and rewrote everything...
It's much more C++'ish, but I can't compile it...

>Just copy/paste, compile and tell me your suggestions.
I'll check out your code in a little bit. I just wanted to mention the below:
>----- unfinished code begins ----
// #include <ctype.h/* for isspace */
// For C++ you want to use:
#include <cctype/* for isspace */

This will put all those things in 'ctype' in the 'std::' namespace. You won't
have to change any of your code because you are using 'using namespace std;'.
(otherwise you would use 'std::isspace()'.)
/************************************************** ***/
>void convert_data(unsigned int linenumber,
const string &cur_ifilename, const string &cur_ofilename){
// ofstream outfile( cur_ofilename::c_str() ); <<<< WRONG???

ofstream outfile( cur_ofilename.c_str() ); // change the qualifier '::' to
dot '.'
// note: ah, I see you did it correctly below.
<snip>

/* """ // note: this is my way of 'quoting' since my newsreader doesn't
// do it for me (you are posting from a *nix machine?)

/************************************************** ***/
void set_file_names(unsigned int filenumber, string &cur_ifilename,
string &cur_ofilename){
/*************************
* Creating file names *
*************************/
ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();
filename_generated << "tex_file" << setfill('0') <<
setw(3) << filenumber << ".tex";
/* WRONG!!! = file000.vtktex_file000.tex ??? */
cur_ofilename = filename_generated.str();

""" */

// OK, when you want to 're-use' a stream you need to 'clear' it:

ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();

// -- add this here: --
filename_generated.str(""); // 'clear' the stream
filename_generated.clear(); // and 'reset' the stream

filename_generated << "tex_file" << setfill('0') <<
setw(3) << filenumber << ".tex";
cur_ofilename = filename_generated.str();

cout << "Input filename = " << cur_ifilename << endl;
cout << "Output filename = " << cur_ofilename << endl;
} // set_file_names(unsigned int, string &, string &) end
/* """

/************************************************** ***/
int main(){
string current_data_type;
vector<stringpossible_data_types;
int data_type;
string read_line;
unsigned int filenumber; /* current data file number */
unsigned int linenumber; /* current line */
unsigned int number_of_lines; /* how many data lines follows */
string cur_ifilename; /* input */
string cur_ofilename; /* output */
/************************************************** *****
* scan the first input file for possible data types *
************************************************** *****/
set_file_names(0, cur_ifilename, cur_ofilename);
ifstream infile( cur_ifilename.c_str() );
while( infile ) {
getline( infile, read_line);

""" */

// Try it this way:

ifstream infile( cur_ifilename.c_str() );
if( not infile ){ /* handle error here */}

while( getline( infile, read_line) ) {
// ......
} // while(getline)
That should clear-up a few things. I'll post again after I 'check-out' your
code.

You may be interested in a little header I've been playing with. It ain't
perfect, but, has worked for everything I threw at it.
[ assume your file is not super-big (will fit in memory) ]

// -------------------------------------------------------
// Filer.h
// -------------------------------------------------------
#ifndef Filer_H
#define Filer_H
// ----------------------------------------------
#include <iostream>
#include <ostream // std::endl
#include <string>
#include <vector>
#include <fstream>
// --------------------------------------
#include <stdexcept>
class FilerErr : public std::runtime_error { public:
FilerErr(const std::string& msg = "") : std::runtime_error(msg){}
};
//use: try{ throw FilerErr(__FILE__": my message");}
// --------------------------------------
// note: AFAIK vector does not have a virtual destructor, so,
// *never* use this class thru a base pointer.

class Filer : public std::vector<std::string{
public:
// Filer(){}
Filer(const char *filename) { Open(filename); }
private: // public: if I use Filer() Ctor. Or, 2nd file open
void Open(const char *filename) throw(FilerErr){
std::ifstream in(filename);
if(!in){ throw FilerErr(__FILE__": Open SonOfABitch! ");}
//if(!in)
for( std::string line; std::getline(in, line); ){
push_back(line);
}
return;
} //Open(const char*) throw(MyError)
public:
void Write(std::ostream& out = std::cout) throw(FilerErr){
if(!out){ throw FilerErr(__FILE__": Write SonOfABitch! ");}
//if(!in)
for(const_iterator w = begin(); w != end(); ++w)
out << *w << std::endl;
} //Write(ostream&)
}; //class Filer
// -------------------------------------------------------
#endif //#ifndef Filer_H
// --- TestFiler.cpp ---
// ----------------------------------------------
#include <iostream// just in case<G>
#include <fstream>
#include <string// just in case<G>
#include "Filer.h"

int main(){
using std::cout; // for NG posting
try{
cout<<"_____ Filer.h test _____"<<std::endl;
Filer Afile("Filer.h");
// Filer Afile("ErrFiler.h"); // to test exception.
Afile.Write( cout );
cout<<"\nAfile.size()="<<Afile.size()<<" lines."<<std::endl;
cout<<"Afile.at(1)="<<Afile.at(1)<<std::endl;

std::string FindThis( "// *never* use this class thru a base
pointer." );
// - the following works, but, needs 'fixing' <G-
Filer::iterator FindMain = Afile.begin();
for(size_t a(0); a < Afile.size(); ++a){
if( Afile.at(a).find( FindThis ) != std::string::npos ){
FindMain = Afile.begin() + a;
}
} // for(a)
Afile.insert(FindMain, "//__________ inserted line __________");
Afile.push_back("\n//----- This is a copy -----");
std::ofstream Ofile("CopyFiler.txt");
if( !Ofile){ return EXIT_FAILURE; }
Afile.Write( Ofile );
cout<<"_____ Filer.h test End _____"<<std::endl;
} // try{}
catch(FilerErr &x){
cout<<"FilerErr &x.what()="<< x.what() <<std::endl;
} // catch(FilerErr&)
catch(std::out_of_range &Oor){
cout<<"\ncaught "<<Oor.what()<<std::endl;
}
catch(...){
cout<< "\ncaught something!!" <<std::endl;
} //catch(MyError&)
return 0;
} //main()
// -----------------------------------------------------------------END

--
Bob R
POVrookie
Oct 14 '06 #12

P: n/a
BobR wrote:
-snip-
>>----- unfinished code begins ----
// #include <ctype.h/* for isspace */

// For C++ you want to use:
#include <cctype/* for isspace */
Thanks.

-snip-
// ofstream outfile( cur_ofilename::c_str() ); <<<< WRONG???

ofstream outfile( cur_ofilename.c_str() ); // change the qualifier '::'
to
dot '.'
// note: ah, I see you did it correctly below.
Dough.... Thanks...
/* """ // note: this is my way of 'quoting' since my newsreader doesn't
// do it for me (you are posting from a *nix machine?)
Yes, I try to learn to use emacs and the linux utilities on my suse 10.1
laptop machine and sometimes the compiler complains if I use
"old-fashioned" // for comments so I manually change all comments to /*
*/-style...
-snip-
// OK, when you want to 're-use' a stream you need to 'clear' it:

ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();

// -- add this here: --
filename_generated.str(""); // 'clear' the stream
filename_generated.clear(); // and 'reset' the stream
Thanks! Very nice... Exactly what I couldn't solve myself...

-snip-
// Try it this way:

ifstream infile( cur_ifilename.c_str() );
if( not infile ){ /* handle error here */}

while( getline( infile, read_line) ) {
// ......
} // while(getline)
That should clear-up a few things. I'll post again after I 'check-out'
your code.
Thanks a lot... That was also a great suggestion as it clears things a bit
up...
You may be interested in a little header I've been playing with. It ain't
perfect, but, has worked for everything I threw at it.
[ assume your file is not super-big (will fit in memory) ]
-snip-

I just quickly tried to compile it and it runs fine... I've always wanted to
learn more about this try/catch-thing. So this program is really great for
that, I think... See my other reply, which I'll post shortly...

Summary: I think I'm almost done with the hardest part, but I only get a
single output-file written out............... Do you have any idea why?
--------- program that's much better than I started out with ------------
#include <iostream>
#include <fstream>
#include <string /* for string manipulations */
#include <sstream/* for std::ostringstream */
#include <iomanip/* for std::setfill(), std::setw() */
#include <vector /* sequential container */
#include <cctype/* for isspace */
using namespace std;
/**************************************************
* *
* This program takes data from some .vtk *
* input-files and exports them to .tex-format *
* *
**************************************************/
/************************************************** ***/

std::string trim(std::string str)
{
string::size_type pos = str.find_last_not_of(' ');

if(pos != string::npos)
{
str.erase(pos + 1);
pos = str.find_first_not_of(' ');
if(pos != string::npos)
str.erase(0, pos);
}
else
str.erase(str.begin(), str.end());

return str;
}
/************************************************** ***/

void convert_data(unsigned int linenumber,
const string &cur_ifilename, const string &cur_ofilename)
{

string LaTeX_header = "\\begin{document}\n";
string LaTeX_tail = "\\end{document}\n";

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

ofstream outfile( cur_ofilename.c_str() );

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

/************************************************** ***/

void set_file_names(unsigned int filenumber, string &cur_ifilename,
string &cur_ofilename)
{

/*************************
* Creating file names *
*************************/

ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();

filename_generated.str(""); /* clear stream */
filename_generated.clear(); /* reset it */

filename_generated << "tex_file" << setfill('0') <<
setw(3) << filenumber << ".tex";

cur_ofilename = filename_generated.str();
}

/************************************************** ***/

int main()
{
string current_data_type;
vector<stringpossible_data_types;
int data_type;
string read_line;

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

string cur_ifilename; /* input */
string cur_ofilename; /* output */
/************************************************** *****
* scan the first input file for possible data types *
************************************************** *****/

set_file_names(0, cur_ifilename, cur_ofilename);

ifstream infile( cur_ifilename.c_str() );
if( not infile )
{
cout << "\n\nERROR!\n**********\nCouln't open file " << cur_ifilename
<< endl;
return 1;
}
while( getline( infile, read_line) )
{

if( read_line.find("SCALARS") != string::npos )
{
/* "SCALARS" was found in read_line */

/* "SCALARS" + spc + |current_data_type| + spc */
// remove the word SCALARS from read_line ?!?!
current_data_type = trim( read_line );

/* I can't figure out how to trim the line correctly */
possible_data_types.push_back(current_data_type);
}
}
/*********************************************
* ask which data type should be converted *
********************************************/

cout << "*******************************************" << endl;
data_type = 0;
for( vector<string>::const_iterator it = possible_data_types.begin();
it != possible_data_types.end(); ++it)
{
data_type++;
cout << data_type << ": " << *it << endl;
}
cout << endl << "Which datatype (number) should be converted ? ";
cin >data_type;

current_data_type = possible_data_types[data_type-1];

/*******************************
* convert data in all files *
*******************************/

for(filenumber=0; filenumber<999; filenumber++)
{

set_file_names(filenumber, cur_ifilename, cur_ofilename);

linenumber = 0;
ifstream infile( cur_ifilename.c_str() );

if( infile )
{

linenumber++; /* counter for debugging */
getline( infile, read_line);

if( read_line == current_data_type )
{
cout << read_line << endl; /* debug */

convert_data( linenumber, cur_ifilename, cur_ofilename);
}
}
else
break;
}
cout << endl << filenumber+1 << " number of .tex files written" << endl <<
endl;
return 0;
}

-------------------------------------------------------------------------
Best regards
Martin Jørgensen

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

Oct 14 '06 #13

P: n/a
BobR wrote:

-snip-
// --- TestFiler.cpp ---
// ----------------------------------------------
#include <iostream// just in case<G>
#include <fstream>
#include <string// just in case<G>
#include "Filer.h"

int main(){
using std::cout; // for NG posting
try{
cout<<"_____ Filer.h test _____"<<std::endl;
Filer Afile("Filer.h");
// Filer Afile("ErrFiler.h"); // to test exception.
Afile.Write( cout );
cout<<"\nAfile.size()="<<Afile.size()<<" lines."<<std::endl;
cout<<"Afile.at(1)="<<Afile.at(1)<<std::endl;

Okay, so far so good.... But what does the following do?

std::string FindThis( "// *never* use this class thru a base
pointer." );
I can't remember that, although I learned something about that... Do you
have a small example of what *not* to do here?
// - the following works, but, needs 'fixing' <G-
Filer::iterator FindMain = Afile.begin();
for(size_t a(0); a < Afile.size(); ++a){
if( Afile.at(a).find( FindThis ) != std::string::npos ){
FindMain = Afile.begin() + a;
}
} // for(a)
Afile.insert(FindMain, "//__________ inserted line __________");
Afile.push_back("\n//----- This is a copy -----");
std::ofstream Ofile("CopyFiler.txt");
if( !Ofile){ return EXIT_FAILURE; }
Afile.Write( Ofile );
Hmm... The above code... Perhaps it's a stupid question, but what does it
do? The catch-part below, I think is easy to understand...
Best regards
Martin Jørgensen

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

Oct 14 '06 #14

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
BobR wrote:

-snip-
>// --- TestFiler.cpp ---
// ----------------------------------------------
<snip>
>Okay, so far so good.... But what does the following do?
> std::string FindThis( "// *never* use this class thru a base
pointer." );
It just 'initialises' the string 'FindThis' to the text to search for.

std::string FindThis( "Hi." ); // initialization
// accomplishes the same thing as:
std::string FindThis;
FindThis = "Hi."; // assignment

>I can't remember that, although I learned something about that... Do you
have a small example of what *not* to do here?
For a std::string or my class 'Filer'?
For 'Filer', you NEVER want to do this:

std::vector<std::string*MyPointer = new Filer("somefile.txt");

If 'std::vector' had a virtual destructor, you might get away with it (for
simplicity here, just think that an vector::iterator is a pointer. So, don't
do that either.). It can cause a memory leak due to 'slicing' (corrections by
experts are welcome.<G>).

Filer *MyPointer = new Filer("somefile.txt");
....should/*might* work, but, that's not what I intended. I keep the class
simple for simple use.
/* """
// - the following works, but, needs 'fixing' <G-
Filer::iterator FindMain = Afile.begin();
for(size_t a(0); a < Afile.size(); ++a){
if( Afile.at(a).find( FindThis ) != std::string::npos ){
FindMain = Afile.begin() + a;
}
} // for(a)
Afile.insert(FindMain, "//__________ inserted line __________");
Afile.push_back("\n//----- This is a copy -----");
std::ofstream Ofile("CopyFiler.txt");
if( !Ofile){ return EXIT_FAILURE; }
Afile.Write( Ofile );
""" */
>Hmm... The above code... Perhaps it's a stupid question, but what does it
do? The catch-part below, I think is easy to understand...
It demonstrates searching through a file ("Filer.h") for a certain text
('FindThis'). Then it 'inserts' a line into the vector (class 'Filer' is
derived from a std::vector, so, it acts like a vector.). Then it adds a line
"-- This is a copy --" to the end (using 'push_back()').
Then it writes the vector out to a file "CopyFiler.txt". You can open that
file in any text editor to verify that the original file and the new file are
different.

Does that 'splain enough?
--
Bob R
POVrookie
Oct 15 '06 #15

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
BobR wrote:
-snip-
>Summary: I think I'm almost done with the hardest part, but I only get a
single output-file written out............... Do you have any idea why?
>--------- program that's much better than I started out with ------------
>std::string trim(std::string str){
<snip>
if(pos != string::npos){
<snip>
}
else
str.erase(str.begin(), str.end());
Did you intend to erase the whole string?
Just use:
str.clear();
return str;
}
/* """

/************************************************** ***/
void convert_data(unsigned int linenumber,
const string &cur_ifilename, const string &cur_ofilename){
string LaTeX_header = "\\begin{document}\n";
string LaTeX_tail = "\\end{document}\n";
/***********************************
* do the actual conversion here *
***********************************/
ofstream outfile( cur_ofilename.c_str() );
outfile << LaTeX_header;
/*
for(unsigned int linenumber=1; linenumber<number_of_lines; linenumber++){
infile >something;
outfile << converted_something;
}
*/
outfile << LaTeX_tail;
}

""" */
/************************************************** ***/
>int main(){
<snip>

/* I can't figure out how to trim the line correctly */

Show us a line, and what you want it to look like.
( put the lines in quotes ("") so we can see the spaces. )
/* """
// ref: string current_data_type;
current_data_type = possible_data_types[data_type-1];
/*******************************
* convert data in all files *
*******************************/
for(filenumber=0; filenumber<999; filenumber++){
set_file_names(filenumber, cur_ifilename, cur_ofilename);
linenumber = 0;
ifstream infile( cur_ifilename.c_str() );
if( infile ){
linenumber++; /* counter for debugging */
getline( infile, read_line);
if( read_line == current_data_type ){

""" */

I think that last line is NOT what you want! Is there a possibility that
there is more than one 'data_type' in a single line?
If not, you may want:

if( read_line.find( current_data_type ) != string::npos ){ /* ... */ }

You were comparing two strings for equality.

std::string Line1("this is a line of text.");
std::string Line2("text");
if( Line1 == Line2 ){
// will never execute this line
}

cout << read_line << endl; /* debug */
convert_data( linenumber, cur_ifilename, cur_ofilename);
}
At least I can see you are makeing progress! <G>
Don't be afraid to put in more 'std::cout', you can easily remove them in
your final effort.
Also, work on one thing at a time. Get one function working good, then work
on the next one.
[ Dang, I wish I could follow my own advice!!<G]

--
Bob R
POVrookie
Oct 15 '06 #16

P: n/a
BobR wrote:
-snip-
/* I can't figure out how to trim the line correctly */

Show us a line, and what you want it to look like.
( put the lines in quotes ("") so we can see the spaces. )
I think it works now, so I might just have one problem left... I hope... See
below...
/* """
// ref: string current_data_type;
current_data_type = possible_data_types[data_type-1];
/*******************************
* convert data in all files *
*******************************/
for(filenumber=0; filenumber<999; filenumber++){
set_file_names(filenumber, cur_ifilename, cur_ofilename);
linenumber = 0;
ifstream infile( cur_ifilename.c_str() );
if( infile ){
linenumber++; /* counter for debugging */
getline( infile, read_line);
if( read_line == current_data_type ){

""" */

I think that last line is NOT what you want! Is there a possibility that
there is more than one 'data_type' in a single line?
Yes, there are many "SCALARS" entries, see output below...
If not, you may want:

if( read_line.find( current_data_type ) != string::npos ){ /* ... */
}

You were comparing two strings for equality.
Actually this is okay, because I was lucky and figured out that the user
could just choose from this vector<stringmenu, like below:

-------
./a.out
Deleting old data files...

*******************************************
From initial data file: number_of_lines = 905

1: SCALARS Temperature double 1
2: SCALARS Porosities double 1
3: SCALARS RhoCp double 1
4: SCALARS Cell_energy double 1
5: SCALARS Residuals double 1
6: SCALARS Solidification_time double 1
7: SCALARS h0 double 1
8: SCALARS hx double 1
9: SCALARS hy double 1
10: SCALARS rx double 1
11: SCALARS ry double 1
12: SCALARS area_porosity_X double 1
13: SCALARS area_porosity_Y double 1
14: SCALARS HTC_interface_length double 1

Which datatype (number) should be converted ? 4
SCALARS Cell_energy double 1
SCALARS Cell_energy double 1
SCALARS Cell_energy double 1
SCALARS Cell_energy double 1
SCALARS Cell_energy double 1

5 number of .TeX files written
-------

So I was lucky because all the "SCALARS" lines (shown above) are in all the
input files so I can just compare two strings for equality and when they're
equal, I know that this is the data-type I want to export and I should then
consider the numbers immediately below that line/location...

But I agree, that wasn't clear because I didn't told that so nobody could
know that...
-snip-
Also, work on one thing at a time. Get one function working good, then
work on the next one.
[ Dang, I wish I could follow my own advice!!<G]
LOL... Yep, I agree... But I have one problem left, I think... My program
doesn't compile now (error in void convert_data()-function):

After that I think/I hope I'm done and then I think I would like to sit and
play around with you try/catch program :-)

----------------
#include <iostream>
#include <fstream /* for file functions */
#include <string /* for string manipulations */
#include <sstream /* for std::ostringstream */
#include <iomanip /* for std::setfill(), std::setw() */
#include <vector /* sequential container */
#include <cctype /* for isspace */
using namespace std;
/************************************************** ***/

std::string trim(std::string str)
{

string::size_type pos = str.find_first_not_of(' ');
if(pos != string::npos)
str.erase(0, pos);

pos = str.find(' ');
if(pos != string::npos)
str.erase(pos);

return str;
}

/************************************************** ***/

void convert_data(unsigned int &linenumber, const unsigned int
&number_of_lines,
const ifstream &infile, const string &cur_ofilename)
{
double read_value;

string LaTeX_header = "\\documentclass{article}\n....";
string LaTeX_tail = " \\end{tabular}}\n\n\\end{document}\n\n";

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

ofstream outfile( cur_ofilename.c_str() ); /* open output-file */

outfile << LaTeX_header;
for(unsigned int line=1; linenumber<number_of_lines; linenumber++)
{
linenumber++;

/* **************************************************
OKAY: When I come to this point the input file looks like:
---
SCALARS something double 1
543.2
342.1
123.5
643.2
....
etc.
----

I want to read the number: 543.2 into a variable, such as read_value, but I
get compiler error...

In function ‘void convert_data(unsigned int&, const unsigned int&, const
std::ifstream&, const std::string&)’:
output_to_latex.cpp:67: error: no matching function for call to
‘std::basic_ifstream<char, std::char_traits<char::getline(std::string&)
const’
......******************************************** ***
*/
string read_line; // <<<<<<<<<<<-------- ERROR
infile.getline( read_line ); // <<<<<<<<<<<-------- ERROR
infile >read_value;

// outfile << converted_something;
}

outfile << LaTeX_tail;
outfile.close(); /* close it */

}

/************************************************** ***/

void set_file_names(unsigned int filenumber, string &cur_ifilename,
string &cur_ofilename, string &current_data_type)
{

/*************************
* Creating file names *
*************************/

ostringstream filename_generated;

filename_generated << "file" << setfill('0') <<
setw(3) << filenumber << ".vtk";
cur_ifilename = filename_generated.str();

filename_generated.str(""); /* clear stream */
filename_generated.clear(); /* reset it */

filename_generated << current_data_type << setfill('0') <<
setw(3) << filenumber << ".tex";

cur_ofilename = filename_generated.str();
}

/************************************************** ***/

int main()
{
string current_data_type;
vector<stringpossible_data_types;
int data_type;
string read_line;

unsigned int filenumber; /* current data file number */
unsigned int linenumber; /* current line */
unsigned int number_of_lines; /* how many data lines follows */
unsigned int number_of_columns;

string cur_ifilename; /* input */
string cur_ofilename; /* output */
string output_data_type; /* part of output-filename */

cout << "\nDeleting old data files...\n\n";

#ifdef _WIN32
system("del /Q *.tex");
#else
system("rm -f *.tex");
#endif
/************************************************** *****
* scan the first input file for possible data types *
************************************************** *****/

set_file_names(0, cur_ifilename, cur_ofilename, output_data_type);

ifstream infile( cur_ifilename.c_str() );

if( not infile )
{
cout << "\n\nERROR!\n**********\nCouln't open file " << cur_ifilename
<< endl;
return 1;
}

linenumber = 0; /* debug */
while( getline( infile, read_line) )
{

linenumber++; /* update */

if( linenumber == 2)
{
istringstream input( read_line.substr( 0, read_line.find('
') ) );
input >number_of_columns;
}
else if( read_line.find("SCALARS") != string::npos )
{
/* "SCALARS" was found in read_line */
possible_data_types.push_back(read_line);
}
else if( read_line.find("CELL_DATA") != string::npos )
{
istringstream input( read_line.substr( read_line.find(' ')+1 ) );
input >number_of_lines;
}
}
infile.close();
/*********************************************
* ask which data type should be converted *
********************************************/

cout << "*******************************************" << endl;
cout << "From initial data file: number_of_lines = " << number_of_lines <<
endl << endl;
data_type = 0;
for( vector<string>::const_iterator it = possible_data_types.begin();
it != possible_data_types.end(); ++it)
{
data_type++;
cout << data_type << ": " << *it << endl;
}
cout << endl << "Which datatype (number) should be converted ? ";
cin >data_type;

current_data_type = possible_data_types[data_type-1];

output_data_type =
trim( current_data_type.substr( current_data_type.find(' '),
current_data_type.rfind(' ')) );
/*******************************
* convert data in all files *
*******************************/

for(filenumber=0; filenumber<999; filenumber++)
{

set_file_names(filenumber, cur_ifilename,
cur_ofilename, output_data_type);

linenumber = 0;
ifstream infile( cur_ifilename.c_str() );

if ( infile.is_open() )
{
while( getline( infile, read_line ) )
{

linenumber++; /* counter for debugging */

if( read_line == current_data_type )
{
cout << read_line << endl; /* debug */

convert_data( linenumber, number_of_lines,
infile, cur_ofilename);
}
}
}
else
break; /* no more files to go, don't try up to 999 */

infile.close();
}
cout << endl << filenumber << " number of .TeX files written" << endl <<
endl;
return 0;

}
----------------
Best regards
Martin Jørgensen

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

Oct 15 '06 #17

P: n/a
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
BobR wrote:
-snip-
>>Hmm... The above code... Perhaps it's a stupid question, but what does it
do? The catch-part below, I think is easy to understand...


It demonstrates searching through a file ("Filer.h") for a certain text
('FindThis'). Then it 'inserts' a line into the vector (class 'Filer' is
derived from a std::vector, so, it acts like a vector.). Then it adds a line
"-- This is a copy --" to the end (using 'push_back()').
Ok, I notice that...
Then it writes the vector out to a file "CopyFiler.txt". You can open that
file in any text editor to verify that the original file and the new file are
different.
Yep, I saw that...
Does that 'splain enough?
Great, thanks... I think it does although I'll have to sit and spend
some time on it later today in the afternoon, hopefully...
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 15 '06 #18

P: n/a

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>
--
Bob R
POVrookie
Oct 15 '06 #19

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
BobR wrote:
-snip-

/* """

/************************************************** ***/
void convert_data(unsigned int &linenumber, const unsigned int
&number_of_lines, const ifstream &infile, const string
&cur_ofilename){
double read_value;
string LaTeX_header = "\\documentclass{article}\n....";
string LaTeX_tail = " \\end{tabular}}\n\n\\end{document}\n\n";
/***********************************
* do the actual conversion here *
***********************************/
ofstream outfile( cur_ofilename.c_str() ); /* open output-file */
outfile << LaTeX_header;
for(unsigned int line=1; linenumber<number_of_lines; linenumber++) {
linenumber++;

/* **************************************************
OKAY: When I come to this point the input file looks like:
---
SCALARS something double 1
543.2
342.1
123.5
643.2
....
etc.
----
I want to read the number: 543.2 into a variable, such as read_value, but I
get compiler error...

In function ‘void convert_data(unsigned int&, const unsigned int&, const
std::ifstream&, const std::string&)’:
output_to_latex.cpp:67: error: no matching function for call to
‘std::basic_ifstream<char, std::char_traits<char::getline(std::string&)
const’
......******************************************** ***
*/

""" */

??? Try removing the 'const' on the ifstream reference, and see what happens.
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).
infile >read_value;
So, you could be trying to put "\n" into a type double var.

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

// ------------------------------------
void Testisdigit( std::ostream &cout ){
cout<<"\n_____[ "<<__PRETTY_FUNCTION__<<" ]_____"<<std::endl;
// ----------
// #include <ccytpe // ISO C++ 14882: <ccytpe>
cout<<"\n_______ isdigit test _______"<<std::endl;
int Inum(0);
double Dnum(0);
std::string TestForNum("Hi 4 76.5num 42.7e-5");
cout<<"Test string = "<<TestForNum<<"."<<std::endl;
for(size_t i(0); i < TestForNum.size(); ++i){
cout<<"char at "<<i<<" is "<<TestForNum.at(i)<<", a ";
if( std::isdigit( TestForNum.at(i) ) ){
std::istringstream strStream( TestForNum.substr(i) );
strStream >Inum;
strStream.clear();
strStream.str( TestForNum.substr(i) );
strStream >Dnum;
strStream.clear();
cout<<"digit Inum="<<Inum
<<" Dnum="<<Dnum<<std::endl;
}
else{ cout<<"alpha"<<std::endl;}
}
cout<<"_______ isdigit test _______Done"<<std::endl;
// ----------
cout<<"_____[ "<<__FUNCTION__<<" ]__End\n"<<std::endl;
return;
} // Testisdigit(ostream&)
// ------------------------------------
/* -- output --
_______ isdigit test _______
Test string = Hi 4 76.5num 42.7e-5.
char at 0 is H, a alpha
char at 1 is i, a alpha
char at 2 is , a alpha
char at 3 is 4, a digit Inum=4 Dnum=4
char at 4 is , a alpha
char at 5 is 7, a digit Inum=76 Dnum=76.5
char at 6 is 6, a digit Inum=6 Dnum=6.5
char at 7 is ., a alpha
char at 8 is 5, a digit Inum=5 Dnum=5
char at 9 is n, a alpha
char at 10 is u, a alpha
char at 11 is m, a alpha
char at 12 is , a alpha
char at 13 is 4, a digit Inum=42 Dnum=0.000427
char at 14 is 2, a digit Inum=2 Dnum=2.7e-005
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.
/* """

/************************************************** ***/
int main(){
string current_data_type;
vector<stringpossible_data_types;
int data_type;
string read_line;

unsigned int filenumber; /* current data file number */
// unsigned int linenumber; /* current line */
unsigned int number_of_lines; /* how many data lines follows */
unsigned int number_of_columns;

string cur_ifilename; /* input */
string cur_ofilename; /* output */
// string output_data_type; /* part of output-filename */
""" */

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.

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

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

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.
--
Bob R
POVrookie
Oct 15 '06 #20

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .

/* """

/************************************************** ***/
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
Oct 15 '06 #21

P: n/a
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

Oct 16 '06 #22

P: n/a
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

Oct 16 '06 #23

P: n/a
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

Oct 16 '06 #24

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>.... 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
Oct 16 '06 #25

P: n/a
BobR wrote:
>
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>>.... 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

Oct 17 '06 #26

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
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
Oct 18 '06 #27

P: n/a
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
-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
Oct 18 '06 #28

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>
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
Oct 18 '06 #29

P: n/a
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>>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
Oct 19 '06 #30

P: n/a

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>
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

--
Bob R
POVrookie
Oct 19 '06 #31

P: n/a
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>>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 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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 19 '06 #32

This discussion thread is closed

Replies have been disabled for this discussion.