• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ectara

C++ Streams versus C Standard I/O Streams

6 posts in this topic

Are there any concrete benefits other than ease of use for the C++ streams over the C file streams? It is much easier to parse files and read text with C++ streams, by being able to get the next character without advancing the file pointer, and other features. However, I am hesitant to declare C++ streams as being more beneficial than C streams, because the standard streams in C++ seem to require the input to be placed into a buffer that the stream interface then uses to get the data; this means that a certain number of elements must be read into the buffer in order to do any reading operation. This has the side effect that the offset of the stream's back end won't actually reflect the logical file position reported by the stream interface. This becomes important if you need to interact with the stream through its interface, then interact with the back end alone, which becomes broken because it is left in an unexpected state; if it must fill a 32 byte buffer, then it would read 32 bytes, and you would read from the buffer. If you get 5 elements from the stream, the buffer's position would only be 5 ahead, but the back end would be 32 ahead, the size of the buffer.

 

Now, this may seem like an unimportant concept, but there is a use case for this, which has many different problems with this method. I have a unique archive format, with a library that allows you to open the files contained within as streams like ordinary files. Additionally, the files may be compressed, and the whole archive can be encrypted, so the archive library manages an encryption stream on top of the stream that contains the archive itself. If the file is compressed, there is a compression stream on top of the encryption stream. On top of everything else, there is an archive stream, which effectively simulates a normal file stream while reading from the stream containing the archive. This may seem overly complex, but it allows one to transparently use a file from an archive, which may be compressed or encrypted, and the modules that read and write data with streams don't know the difference. This makes it possible to read an image from an encrypted archive using the same function that reads an image from a file on disk.

 

My point isn't to debate how the stream hierarchy works; it is a successfully implemented extensible framework using a stream model like C's streams. The thing is, using an interface like C++ streams that require a stream buffer would prevent one from having any control over where the file pointer of the back end actually is, and would prevent you from having unbuffered streams.

 

So, put another way, how is the C++ stream model not problematic? Does the issue of unbuffered streams ever come up for anyone? How about non-rewindable streams? I had a stream type at one point that paired a font and a frame buffer, which would allow you to write characters to the stream, and it would be printed to the frame buffer. Being that the stream was a static image and a raster position, there was no way to measure how far back to move, or overwrite already written characters. As a result, the stream is unrewindable. There are also examples of unrewindable input streams. If the stream can't be rewound, and it is used in a buffered stream as described above, then the stream will be positioned further ahead than the logical offset in order to fill the buffer, and when all is said and done, the characters that were unused between the logical offset and the physical offset of where the buffer ended are lost, because you can't seek backward.

0

Share this post


Link to post
Share on other sites
You seem to know more about this than I do, but can you not get unbuffered C++ streams by calling setbuf(0,0)? Edited by Álvaro
0

Share this post


Link to post
Share on other sites

You seem to know more about this than I do, but can you not get unbuffered C++ streams by calling setbuf(0,0)?

I may not know as much about the streams as I could, but technically, that isn't required to do anything. For file streams, setbuf(0, 0) must make the stream unbuffered, but for most uses, it is implementation defined, and for stream buffers, setbuf(0, 0) is required to do nothing. So, there's nothing stopping the stream from maintaining a separate buffer from the string stream, and having the same synchronization problems. It is only defined to work on file buffers, one of many possible kinds.

 

However, the question concerning the standards is more of a hypothetical one; I'm rewriting my old stream modules from C using new paradigms and models, and I'm wondering whether I should keep the old design, which was based on C's streams, or adopt something more like C++'s streams. As far as advantages go, using C++ streams to parse text files character by character is incredibly easy by being able to read the current character without moving the file pointer ahead. In C++, if you make a file buffer unbuffered, it still provides the ability to to get the current character without advancing the file pointer, which is something I wouldn't be able to implement in many stream types without buffering the input, or using unget functionality. Unfortunately, without a standard way for me to read the current character without moving the file pointer while not redundantly using C++ streams as the back end, I'd run into the same synchronization problems as before, because I'd have to read the current character, advancing the file pointer, then either pretend I didn't move the file pointer, or try to seek backwards. The first would make the children streams unsynchronized; they'd be one character ahead of the topmost stream interface, since they don't know why the character was read, only that it happened. The second would break unrewindable streams.

If I could figure out a way to be like C's streams, and also read the current character without advancing the file pointer, that would solve this whole problem. The main benefit that I see is being able to easily get the current character without advancing; since one of the stream types doesn't support it in a standard fashion that I know about, it can't be part of the base stream interface from which all of them inherit.

0

Share this post


Link to post
Share on other sites
Given that the C file streams don't have a mechanism to replace the back end at all, I don't see why you think that not being able to predictably interact with the back end in the C++ streams would be a disadvantage for C++ streams. It seems extremely illogical to state that product A is inferior to product B because product A won't let you do something that you can't do with product B.
0

Share this post


Link to post
Share on other sites

Given that the C file streams don't have a mechanism to replace the back end at all, I don't see why you think that not being able to predictably interact with the back end in the C++ streams would be a disadvantage for C++ streams. It seems extremely illogical to state that product A is inferior to product B because product A won't let you do something that you can't do with product B.

Right, but I only ever use C streams as a back end. Are you just saying that this is a task that cannot be attempted?

To clarify the clarification, I use my own stream model, which is a third and completely separate stream model in regards to the two standardized ones mentioned here. I do have a file stream type, that uses C standard I/O to manipulate files using my stream interface.

Edited by Ectara
0

Share this post


Link to post
Share on other sites

FWIW: in my case I wrap most filesystem stuff in a custom VFS layer.

 

internally, it is more object-based (and new types of file-like objects can be created), but uses a C-like front-end API (it is actually very similar to stdio, just with a name prefix added). however, the backend is more extensible, where new types of files and filesystems can be added as-needed (the VFS backend is actually a little more like the Linux VFS, using a hierarchy and mounting things at various mount-points, with "FS drivers" generally defining both the behavior at the mount-point, and also internally managing file-types specific to this FS type, such as for mounting an OS directory or a ZIP archive).

 

like, the FS driver will register itself with the VFS, providing a call to mount a filesystem, and the VMount interface provides things like open/opendir/stat/... open may return a VFile, which contains any methods to read/write/seek/close/... within the file, ...

 

most of this stuff is kept hidden internally though (where code will just open/read/write/... files, without needing to worry too much about things like VFS mounts).

 

 

theoretically, a person could also do it Java-style, but IMHO this adds a lot more code and complexity to the API without making really a huge improvement in usability.

 

another alternative is, granted, just doing something like having a File class or interface (say, an IFile abstract base class) containing a subset of a stdio-like API. if needed, this could be further wrapped to implement more specialized IO interfaces.

 

admittedly, I am not really personally a huge fan of the design of iostream.

Edited by cr88192
0

Share this post


Link to post
Share on other sites

I wasn't a fan of it, either. It seems like it greatly over-complicated putting characters in a stream, but I can see why some decisions were made. After thinking long and hard, I came to realize that my I/O streams function almost just like C++ iostreams, just implemented in C. I had a self-allocated structure holding details of the stream's back-end data storage, which is referenced through an opaque void pointer in the I/O stream structure, and the stream had an operations structure, which was a pointer to a table of function pointers that perform the lowest level functionality that is unique to that stream type, when passed the opaque pointer.

 

This is just like a stream buffer, just implemented without fancy constructs like inheritance, virtual functions, and other odds and ends of polymorphism.

 

I also came to see the merit of having two stream positions, after realizing that the fstream seekg() and seekp() are allowed to both move a single pointer internally, which makes the most sense for a file that traditionally only has one pointer. I was sold on allowing two stream positions, but most importantly, it being allowed and, to a certain extent, expected that some types might only have a single internal pointer.

 

Right now, with all of these revelations in mind, I'm trying to work toward a rewrite using C++'s new language features, and adding support for a peek() function. For standard I/O streams, it seems the only way to provide this is to use ungetc(), because seeking backwards might not be possible. As a result, it seems that I will have to add the stipulation that the standard file stream is flushed before and after it is attached to one of my stream front-ends, which would discard any ungotten characters, and only use the one guaranteed character of ungetc() for peek() operations; actual unget() functionality in my streams is handled separately, so the use of standard I/O's ungetc() would be feasible, given that the user adheres to the promise that they won't try to use it while the standard stream is the child of one of my streams.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0