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,61724 00
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 4 1574
On Fri, 23 Jan 2004 16:52:29 -0800, "Tad Johnson" <h2********@cox.dot.net> wrote: 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,6172 400
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::ofstream 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()
{
// ...
}
<stdio.h> is what I was using, sorry.
Thanks very much for your help! I will try it in
the way you described.
Tad
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@News.CIS.DFN.DE... On Fri, 23 Jan 2004 16:52:29 -0800, "Tad Johnson" <h2********@cox.dot.net>
wrote: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
toC++. 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
beingused for text/bin string/char manipulation swo I figured I would try my
luckon 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,6172 400
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::ofstream 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() { // ... }
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 ;
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
"Keith H Duggar" <du****@mit.edu> wrote in message
news:b4*************************@posting.google.co m... 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 ; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jeff |
last post by:
Hello.
Quick background as I dont wish to hog anyones time. I am a customer of a
PHP/SQL programmer. A website that I had the programmer build for me is
working great, with one exception.
I...
|
by: Pudibund |
last post by:
Ok, I've spent nearly a week trying to sort what should be an easy
task to accomplish but I'm totally flumoxed!
I want to do something pretty simple...
1. display image1
2. wait until...
|
by: Chris Gordon-Smith |
last post by:
I am tying to call a Pascal function from C++, and vice versa.
Does anyone know how to do this, or where detailed information on this topic
can be found?
For the C++ to Pascal call I have...
|
by: chris kramer |
last post by:
i have an application that allows you to Select some text in a window,
but no option to Copy it to the clipboard (nor does Ctrl-C or
Shift-insert work, or right click etc..) i want to get these...
|
by: VB Programmer |
last post by:
I have this xml file...
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.somesite.com/mypic.jpg</image>
<caption>Picture 1 is here</caption>
</pic>...
|
by: gwlemyre |
last post by:
I am not a PASCAL programmer and I saw this piece of code.
procedure Expression; Forward;
procedure Factor;
begin
if Look = '(' then begin
Match('(');
Expression;
Match(')');
end
|
by: Piripiccio |
last post by:
Hello , please give me a little minute for this problem
the first thing ..... my english is very bad
I wrote a little program that using a socket pair with 2 process Padre
(Father) and Figlio...
|
by: dhruba.bandopadhyay |
last post by:
I am trying to port an old Pascal DOS game to DOS C/C++. I am wondering
if anyone is familar with the dos & crt Pascal units and whether there
are C/C++ equivalent libraries. Maybe dos.c & crt.c?
...
|
by: dhruba.bandopadhyay |
last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer
versions of compilers stop support for the oldskool DOS routines. Am
trying to convert/port an oldskool Pascal program that...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |