Thread: #defines ID's for 3 different compilers + 3 different versions of FILE structure

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Redmond, WA
    Posts
    6

    #defines ID's for 3 different compilers + 3 different versions of FILE structure

    I did some research ahead of time, and want to confirm it, as well as hopefully make my program work.

    Okay, so I am working with Borland C++ 5.6 (don't know if it is C++ Builder 6.0 or CBuilderX), MSVC 8.0, and g++ 3.4. I want to check that my code is handled properly under each compiler using a #ifdef directive.
    Here is what I found:
    __BORLANDC__
    _MSC_VER
    __GNUC__ and __GNUG__ (C and C++)

    Please confirm if you're able to. I am mostly only sketchy on __BORLANDC__ at the moment.

    Now, the reason I would like to know this, is so that I can create a macro for each compiler's implementation of the FILE (typedef-ed from _iobuf in MSVC) structure in stdio.h. I need to skip over characters as needed inside of a file, but want to do so without a call to fread(), and certainly without an fseek(), ftell() combination. Thus, I am hoping to modify the location that a pointer member is directed at (if it exists across all implementations). I would like to know the name of the equivalent member of each structure, so that I can use it under three different names. In MSVC, the element I am looking to use is _ptr, and is a char *. I have found two char *'s that could be what I need in Borland (curm I believe, and buffer). I would suspect that one of these is what I need, but I can't remember if it worked when I had access to a Borland compiler earlier. I feel like I saw it work at some point. Now, under g++, things get a little crazy. The stdio.h that I am using falls under /usr/include/sys I believe (that or some other folder than sys and mingw). FILE is typedef-ed from __FILE, which is typedef-ed from possibly 2 different structures. I found what looked to be two different char *'s as well, but neither seemed to work (could have been that my above work wasn't done yet, and I had messed something up). For all I know, there isn't an equivalent inner pointer to where you are in a file (could be just a count up from the start of the file, and constant adjustment of the main pointer). Considering that I don't have anybody informed on the subject to ask, I just have to ask around here, and hope for a knowledgeable answer. =)

    I should mention that I might need this by Thursday 10/26, since it is for an assignment. However, I already coded around the issue with fread(), where I just don't use the data that is transferred in. Regardless, I would like to know for future reference. Thank you very much! =)

    Example code attached

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why? Why not fseek? I don't see the point in what you're doing at all.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but want to do so without a call to fread(), and certainly without an fseek(), ftell() combination.
    Why?
    No seriously, WHY?

    Why on earth would anyone bother to go to the trouble of deliberately trying to break a perfectly portable abstraction of a FILE by doing what you're trying to do?

    Even assuming for a moment you manage to pull off this trick and get something to compile, there's no reason to believe that
    - it would work at all in the current version. Something is pretty likely to object to you playing with the private parts of someone elses data.
    - it would work at all with any other compiler, or any patch to your current compilers.

    The mind boggles.

    > Location: Redmond, WA
    Heh, must be something in the air then which creates crap code.

    Just use the FILE* API and stop worrying about it!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Redmond, WA
    Posts
    6

    =)

    It is part of the assignment that we can't use fseek() and ftell(). I said certainly not those as to not get that as the solution, since I already knew that it would work.

    I know that my code would be limited to only the three compilers, and possibly only under the version of each that I am compiling with. It is only an assignment, and I wrote it both ways (straight forward, and tricky). I figured I could do a bit of assumptive optimization at the risk of not checking for errors, even though it wouldn't be portable. Plus, I wanted to learn something new. Not to mention this behooved me as an amateur programmer of only about one year to do my own research and find the answers. It is a valuable skill to know how to find information quickly and effectively if you don't know what you're doing. It also allows you to learn through trial and error.

    Indeed, I actually live across from Microsoft's campus. I am not affiliated with them in any capacity, but rather am attending DigiPen Institute of Technology. I have had a bad run of instructors as of yet in the way of learning and haven't done much learning outside of school on my own. Thus, I am prone to do some crazy stuff when I am not certain what I should be doing (in this case, not necessarily wasting time finding a trick, and instead doing it the normal API way). =) This was pointed out by those more knowledgeable than I before, so it isn't really anything new to me. I will leave it at that. If you don't have the answer to what I asked, then please don't make anything more of this if you would. Thank you. =)

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by MoonlightKnight
    Not to mention this behooved me as an amateur programmer of only about one year to do my own research and find the answers. It is a valuable skill to know how to find information quickly and effectively if you don't know what you're doing. It also allows you to learn through trial and error.
    True. It is just so sad that so many instructors spend so much of the time teaching what not to do. After doing 20 assignments containing 30 things not to do, is the student aware of these 30 things? Or did the instructor just churn out another bad programmer? That poor student may not know the 30 things, and here we rather despise that -- when the student goes away remembering most of the don'ts and few of the dos.

    FWIW: http://predef.sourceforge.net/precomp.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Redmond, WA
    Posts
    6

    Thank you!

    That is super useful. =) Thank you very much for posting it. It seems like a good page to save for future reference.

    As for my assignment, I finally got my code running under all three compilers with the same end result without the little trick I was hoping to use. I might still go back and try the compiler trick later on after I am caught up on my other courses. Midterms are this week, so I can't sink any more time into it than I already have. =)

    Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM