Help for a Pascal programmer please?

T

Tad Johnson

Hi all,

I would not normally post about this issue but after a few hours of
struggling maybe it's time for some help. I am a pascal programmer moving to
C++. I am learning from a couple of books, one of which is Wrox Press's
"Beginners guide to C++". I am at a point where simple std.h header is being
used for text/bin string/char manipulation swo I figured I would try my luck
on a small edit project.

I am trying to take this string:

20040120,35.34000,36.29999,35.11999,36.21000,6172400

from the file AA.ASC, and change it to

AA,20040120,35.34,36.29,35.11,36.21,6172400

Structured changed to two digit prescision and begining of file name
appended
to beginning of line, within normal comma separated and lines perminated by
'\n'.
I am having major issues trying to figure out syntax for the frintf()
command. And
the books I am working with give very little clue to what I am doing wrong.
Should I used a character by charater approach outputting each character
into the write
file, or should I take it section by section with comma being separation
test in a do/while loop?
Any input?

#include <stdio.h>

int main ()
{
FILE * rFile;
FILE * wFile;
char string [100];

rFile = fopen ("AA.ASC","r"); //read file for input


if (rFile == NULL) perror ("Error opening file"); //no data for input,
stop
else {

fgets (string , 100 , rFile); //get string of max length of 100


}

wFile = fopen ("AA.txt", "w"); // create file AA.txt for output
if (!wFile) perror ("Error opening file"); //error if problem
else {
fprintf (wFile, "AA,%l, %f, %f",string); //output string formatted and
written
fclose (rFile); //close read file
fclose (wFile); //close write file
}
return 0;
}


Thanks much,

Tad
 
A

Alf P. Steinbach

I would not normally post about this issue but after a few hours of
struggling maybe it's time for some help. I am a pascal programmer moving to
C++. I am learning from a couple of books, one of which is Wrox Press's
"Beginners guide to C++". I am at a point where simple std.h header is being
used for text/bin string/char manipulation swo I figured I would try my luck
on a small edit project.

There is no "std.h" header in standard C++.


I am trying to take this string:

20040120,35.34000,36.29999,35.11999,36.21000,6172400

from the file AA.ASC, and change it to

AA,20040120,35.34,36.29,35.11,36.21,6172400

Structured changed to two digit prescision and begining of file name
appended to beginning of line

Split line on commas. Format each resulting string. Concatenate.

std::string is a good choice for representing the strings.

std::ifstream is a good choice for the input file.

std::eek:fstream is a good choice for the output file.

No string splitter in the standard library, as far as I know; although
you could adapt some of the functionality in the standard library it's
simpler to just write a string splitter function. A good choice for the
result of that function is std::vector<std::string>, a vector of strings.



, within normal comma separated and lines perminated by '\n'.
I am having major issues trying to figure out syntax for the frintf()
command.

fprintf belongs to the C library and is not typesafe.

As a beginner: use the C++ library.

Here's a sketch for you:


#include <vector>
#include <string>
#include <fstream> // or whatever it was, for ifstream and ofstream

typedef std::vector<std::string> StringVector;

StringVector const split( std::string const& s )
{
// ...
}

std::string format( std::string const& s )
{
// ...
}

int main()
{
// ...
}
 
T

Tad Johnson

<stdio.h> is what I was using, sorry.

Thanks very much for your help! I will try it in
the way you described.

Tad

 
K

Keith H Duggar

Hi Tad,

While learning to use streams with a simple parse such as this my
opinion is that you don't need the complication of a splitting
function, vectors, etc. When you extract number types (int,float,...)
from the stream it will read all appropriate digits and stop at
characters such as a comma. Therefore, you can simply extract the
number and then the separator. Here is a complete solution that writes
the transformed string to standard out :

#include <fstream>
#include <iostream>

int main ( int argc , char * argv[] {

std::ifstream input ( "AA.ASC" ) ;

int itemp ;
char comma ;
double ftemp ;

input >> itemp ;
std::cout << "AA," << itemp ;

std::cout.precision ( 2 ) ;
std::cout.flags ( std::ios::fixed ) ;

for ( int i = 0 ; i < 4 ; ++i ) {

input >> comma >> ftemp ;
std::cout << comma << ftemp ;

}

input >> comma >> itemp ;
std::cout << comma << itemp ;

return 0 ;

}

In this case I hard-coded the file name and output stream but of
course in the future you will probably want to improve upon this.
Also, it differs slightly from your requirements because streams ROUND
the decimal rather than truncating it. I figured that's probably what
you actually wanted; but, if you really want to truncate then alter
the code as follows :

input >> comma >> ftemp ;
ftemp = 0.01 * int ( 100.0 * ftemp ) ;
std::cout << comma << ftemp ;

or better :

input >> comma >> ftemp ;
ftemp = 0.01 * static_cast<int> ( 100.0 * ftemp ) ;
std::cout << comma << ftemp ;
 
T

Tad Johnson

Thanks, this is a very clear example, and very much appreciated.
It appears to be much more simple as well. Coming from Pascal
I feel it's been confusing sometimes in terms of syntax etc. But
I am making fast progress and enjoying it very much. I am
starting to see the power in it as well.

Thanks again,

Tad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top