As the tech world constantly blazes forward with new innovations, sometimes it is necessary to teach a new dog old tricks. In this article, wyldeling does just that as he shows the reader how to take a modern language like C++ and make it act a little more like Fortran, which remains in common use within the physics community.
From the article, "despite the power and ease of use of the streams library in C++, it is difficult to read-in fixed width fields without modifying the streams' behavior, unlike in Fortran." Follow along as wyldeling tackles this problem and provides the necessary code to make C++ a little friendlier for Fortran aficionados.
I honestly never knew that FORTRAN was still used anywhere. I remember taking a required semester of it in college (20 years ago) along with PASCAL and BASIC, but I just assumed it went by the wayside a long time ago. Incidentally I can remember my teacher telling us that he was a programmer for NASA, and some of the programs he wrote in the late sixties were still in use on the space shuttle at the time.
Reading fixed width data sounds like the type of thing I used to do to convert information from a Hewlett Packard 3000 mainframe computer to PC’s so we didn’t have to rely on the MIS dept for every manipulation of data we wanted. I had taught myself C language at the time, and had some mechanism where I stored the “widths” in a text file which would be read at runtime. If the MIS dept changed the field widths, I only had to change the text file instead of recompiling the program. I am going to try to take a close look at what you’ve done with templates and overloaded operators because it is an interesting approach.
Some of you may have noticed that the article/code changed over the weekend. Much to my chagrin, I found two serious bugs and a peculiarity of gnu c++ std library. The first of the two bugs involved my implementation of ignoreToEOL. The original version just plain could not work. I believe it was because the construct:
fin >> ignoreToEOL;
was trying to discover to many types at once, and was just getting confused. So, I swapped out that implementation with the a call to ignoreLines(1) which does the same thing, except in order to use it you need to write:
fin >> ignoreToEOL();
instead. The second, and more serious, type of bug (yes, there was more than one) involved the problem of whitespace.
Naively, I assumed passing in " 98" to num_get.get() would return 98, but it barfed setting the fail bit. Passing in "98 " would do the same. Both of these were fairly common cases, and I didn't test for them. So, I needed to trim excess whitespace. It was at this point, I found that using a string was a bad idea. The stream would strip off leading and trailing whitespace, but it would leave the trailing whitespace in the buffer to be grabbed by the next field. So, I switched to loading the characters directly into a character array allocated off the heap. This, also, necesitated the change to the fixed width string functions to guarantee that I got the exact number of characters I asked for.
After using this for a little bit, I noticed that it would not compile if I tried to load in an int. It turns out the gnu c++ std lib's version of num_get does not have a function for loading in a int. It loads in an unsigned int and a long int just fine. But, if you try to load in an int without using my manipulators, it works just fine. So, I assume when you load in an int without using my library, it can perform the implicit cast required, but if you use my library, it cannot. Therefore, don't use an int with this library use a long instead.



article
by 
Add a Comment (7)
Email This
Message Author
Statistics
RSS


The Science Community and Fortran by markmcb :: NR7 :: Show
wyldeling, could you (or anyone else) shed some light on the use of Fortran in the science community? I'm curious if it's got special features that other languages don't have, or if it's just been used for so long that it's just become a standard. I don't use compiled languages too much, so most of my knowledge is in scripting. Anyway, just curious.