Character buffer question

Heh, actually it's a pretty broad question.

What is a character buffer exactly? Someone told me that if I want to make a program that lets the user type in any command word and have the program execute the command, such as (or return a message saying it doesn't know what the user is talking about) I need to use a character buffer.


The only problem is that I do not know what a character buffer is. I do not know the syntax to create one. I don't know ANYTHING about it at all (which is a shame)

If anyone out there is feeling patient and would like to write me some really specific directions and maybe a few samples of code I would greatly appreciate it.

Thanks alot!


~Penance


Comments

  • : What is a character buffer exactly? Someone


    A 'buffer' is usually storage space for (typically) a whole bunch of something. Usually this something is coming in or going out, usually kept in some sort of order. A character buffer then is storage space for a bunch of characters (for example an array or a dynamically allocated chunk of memory being used as an array).




    : told me that if I want to make a program that lets the user type in any command word and have the program execute the command, such as (or return a message saying it doesn't know what the user is talking about) I need to use a character buffer.




    Well, this kinda makes sense, since, if you're taking in user input as typed characters, you're going to need to stash them somewhere, in the order in which they're received, until the user hits return or something of that sort or until your buffer's full of valid data.



    : The only problem is that I do not know what a character buffer is. I do not know the syntax to create one. I don't know ANYTHING about it at all (which is a shame)




    Well, how about this:




    #define BUFFER_SIZE 128

    char acMyBuffer[BUFFER_SIZE];




    There you go, that's a character buffer. You'd probably start filling it from index 0. You might want a number-of-characters-in-buffer value:


    int iMyBufferCurrentSize=0;


    or you may wish it to be null-terminated (ends with a zero byte). In the latter case, it is considered a C-style string of characters. You could always do both.


    You could dynamically allocate yourself a buffer.


    char *pszMyString=(char *)malloc(sizeof(char)*BUFFER_SIZE);


    only you'd have to remember to free it when you're done at the end of your program:


    free(pszMyString);


    You could also use the C++ new and delete keywords to do the same sort of thing.


    You could even do a linked-list of characters. Or even a single character (buffer of one character?). Perhaps a binary tree of characters? I'm not sure how this would work.


    A buffer's a word to represent storage space for incoming (or outgoing) data. Its an I/O word. A concept, really.



    : If anyone out there is feeling patient and would like to write me some really specific directions and maybe a few samples of code I would greatly appreciate it.




    Hope this helps.


    : Thanks alot!

    Sure. Let me know how your project works out. I like getting feedback.


    : ~Penance

    Michael




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories