By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

buffers, streams, confusion

P: n/a
Hello all,

Let me introduce myself. I would say that I am an intermediate
programmer in C++. I am studying comptuer science in college. However,
I have never had a formal introduction to buffers. Here is my problem:

I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.

The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.

Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.

Maybe a std::map is what I should use.

Here is an example:

the config file has:
---------------------
| -user joe
| -pwd football
|____________
run the program with additional cmd line args

C:\ prog -server 127.0.0.1 -port 999

the server, port, user, and pwd need to be in the "buffer". This is
why a map might work but could be overkill.

Thanks for any help,

31337one

Jun 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
31******@gmail.com wrote:
I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.

The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.

Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.


A "buffer" doesn't necessarily have to be a C-style string. I take
"buffer" to mean basically just a temporary storage area where you can
hold things.

One idea would be to use a std::vector<std::string>. If you went this
route, then you do not have to worry about how much memory to allocate
since these containers (std::string can be thought of as a container of
characters) handle that for you.

Also note that in C++, you usually allocate memory with new or new[]
instead of malloc(), though in certain situations you may really need to
use malloc().

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 20 '06 #2

P: n/a
31******@gmail.com schrieb:
Hello all,

Let me introduce myself. I would say that I am an intermediate
programmer in C++. I am studying comptuer science in college. However,
I have never had a formal introduction to buffers. Here is my problem:

I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.
When command line arguments are allowed to override config file entries,
you would have to read the config file first.
The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.
sizeof() does not work on c-style strings. strlen() is for this.
Since you are writing C++, you should use new[] instead of malloc().

Well, better don't use c-style strings and plain pointers directly. Use
std::string for your "buffer", then you don't have to think about its
length. The string class handles the dirty details.
Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.

Maybe a std::map is what I should use.


std::map is exactly what you want. Learn more about the C++ standard
library, it can do a lot of things for you.

Thomas
Jun 20 '06 #3

P: n/a
I do like the map idea and will probably try to use that. I have used
maps, vectors, and such. I am just brainstorming on how to encapsulate
my command line arguments and .conf file and have them in one place.

Another problem I am running into now with the map idea is some flags
for my program take more than one argument.

Example:

prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.

If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?

Thank you,

31337one

Jun 20 '06 #4

P: n/a
<31******@gmail.com> wrote in message
news:11**********************@r2g2000cwb.googlegro ups.com...
I do like the map idea and will probably try to use that. I have used
maps, vectors, and such. I am just brainstorming on how to encapsulate
my command line arguments and .conf file and have them in one place.

Another problem I am running into now with the map idea is some flags
for my program take more than one argument.

Example:

prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.

If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?

Thank you,

31337one


Well, if you used a vector you could build 3 entries
-search xyz
-search wtf
-search bbq

a map wouldn't allow this because the key needs to be unique.
Jun 20 '06 #5

P: n/a
In article <1150843946.496349.170730
@r2g2000cwb.googlegroups.com>, 31******@gmail.com says...

[ ... ]
prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.
You can. Alternatively, you can use a multimap instead of
a map. In this case, you'll probably want to use
equal_range to find your values.
If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?


One easy way is to create a stringstream, and then read
the individual values from it -- but a multimap will
usually be cleaner.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 21 '06 #6

P: n/a
Well thank you all for your support and ideas.

What I was intending to do in the begining was have the map act as a
holder for the different flags on the command line and in a config
file. Then when I actually create the class that would act on the
flags, it would just ask the map for the values (username,
password....).
Instead, I nixed the whole map idea and just updated the class at parse
time.
I didnt want to do this because I thought it would be messier than the
map but the map just turned up to be twice as much code.

Thanks you all again,

31337one

Jun 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.