473,782 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help for a Pascal programmer please?

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.340 00,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

Jul 22 '05 #1
4 1596
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.34 000,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::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()
{
// ...
}
Jul 22 '05 #2
<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.C IS.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.34 000,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::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()
{
// ...
}

Jul 22 '05 #3
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.preci sion ( 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 ;
Jul 22 '05 #4
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.goog le.com...
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.preci sion ( 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 ;

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2249
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 have limited programming experience, most of it Pascal/C++ and I haven't programmed for many years now even then it was mediocre programming of small this and thats.
0
1615
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 image1 is drawn to the screen
4
4393
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 tried declaring the Pascal function as:- extern "C" void PASCALTESTFUNC1(); within my C++ code. However, the linker is giving me an unresolved
3
2950
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 values somehow. below is the best posted method i have found online. if anyone can elaborate, as to how to implement such message passing (as a novice) i would be thrilled. i am using and old visual c++ 4 compiler with an example program that...
2
3336
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> <pic> <image>http://www.somesite.com/mypic2.jpg</image>
2
2165
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
3
1968
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 (Son) but the select see the future . This is the code ... please help me before i become crazy .....
6
460
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? Below lists names of variables, functions, types & weird interrupt procedures found in Pascal. Am wondering what can be done to get around them for use in C/C++. dos.pas crt.pas
0
1583
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 uses registers/ interrupts into C/C++. 1. What are the inline($FA); & inline($FB); Pascal functions? What is the C/C++ equivalent? inline($CD / $1C); inline($9C);
0
9639
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.