Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get(char*, num, delim) question

Reply
Thread Tools

get(char*, num, delim) question

 
 
Phrank
Guest
Posts: n/a
 
      05-23-2004
On 22 May 2004 23:56:12 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Siemel Naran)
wrote:

>Francis Bell <(E-Mail Removed)> wrote in message
>
>> sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
>> f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

>
>> void readInFirstChars(ifstream &fin)
>> {
>> char* first;
>> first = new char[2];

>
>It's fine to just say
> char first[2];
>and remove the delete statement below.
>
>> while (fin.good())
>> {
>> fin.get(first, 4, '/');

>
>Why is first of 2 chars but you read 4 chars into it?
>
>> cout << first << endl;
>> fin.ignore(80, '\n');
>> }
>> cout << "Here first" << endl;
>> if (first == "sp") {

>
>You're comparing the value of the pointers (ie. comparing the memory
>address locations). Use std::strcmp from <cstring> (same as strcmp
>from string.h).
>
>> cout << "Here second" << endl;
>> Spinnerbait temp;
>> readInASpinnerbait(fin);
>> temp.outputSpinnerbait(cout);
>> }
>> delete [] first;

Thanks John and Siemel. I'll go back in and try changing it to an
array. However, the documentation for the parameters for that form of
get() function show that the first parameter needs to be a char*, but
I'll try the array. Also, the documentation says that for the second
parameter (num), it reads up to num-1 characters of the input stream,
or until the delimeter (third parameter) is reached or EOF is reached.
So, I have at most a two character first field with a third character
being the '/' delimeter for a total of 3 characters for the function
to read. So I simply figured it needed num - 1 and I needed 3 so
that's where I came up with 4. And I've used the strcmp before, but
completely forgot about it. Thanks! I'll get back with you and let
you know how things work out.

Frank
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-23-2004
> Thanks John and Siemel. I'll go back in and try changing it to an
> array. However, the documentation for the parameters for that form of
> get() function show that the first parameter needs to be a char*, but
> I'll try the array.


Yes, array are autoamtically converted to pointers, so any function which
say it needs a pointer can be given an array.

Also, the documentation says that for the second
> parameter (num), it reads up to num-1 characters of the input stream,
> or until the delimeter (third parameter) is reached or EOF is reached.
> So, I have at most a two character first field with a third character
> being the '/' delimeter for a total of 3 characters for the function
> to read. So I simply figured it needed num - 1 and I needed 3 so
> that's where I came up with 4.


4 is fine, but the size of the array and the size you pass to get must be
the same. You are telling get that you have four characters in the array,
but actually you've only got two.

john


 
Reply With Quote
 
 
 
 
Francis Bell
Guest
Posts: n/a
 
      05-24-2004
Phrank wrote:
> On 22 May 2004 23:56:12 -0700, (E-Mail Removed) (Siemel Naran)
> wrote:
>
>
>>Francis Bell <(E-Mail Removed)> wrote in message
>>
>>
>>>sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
>>>f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

>>
>>>void readInFirstChars(ifstream &fin)
>>>{
>>> char* first;
>>> first = new char[2];

>>
>>It's fine to just say
>> char first[2];
>>and remove the delete statement below.
>>
>>
>>> while (fin.good())
>>> {
>>> fin.get(first, 4, '/');

>>
>>Why is first of 2 chars but you read 4 chars into it?
>>
>>
>>> cout << first << endl;
>>> fin.ignore(80, '\n');
>>> }
>>>cout << "Here first" << endl;
>>> if (first == "sp") {

>>
>>You're comparing the value of the pointers (ie. comparing the memory
>>address locations). Use std::strcmp from <cstring> (same as strcmp

>
>>from string.h).

>
>>>cout << "Here second" << endl;
>>> Spinnerbait temp;
>>> readInASpinnerbait(fin);
>>> temp.outputSpinnerbait(cout);
>>> }
>>> delete [] first;

>
> Thanks John and Siemel. I'll go back in and try changing it to an
> array. However, the documentation for the parameters for that form of
> get() function show that the first parameter needs to be a char*, but
> I'll try the array. Also, the documentation says that for the second
> parameter (num), it reads up to num-1 characters of the input stream,
> or until the delimeter (third parameter) is reached or EOF is reached.
> So, I have at most a two character first field with a third character
> being the '/' delimeter for a total of 3 characters for the function
> to read. So I simply figured it needed num - 1 and I needed 3 so
> that's where I came up with 4. And I've used the strcmp before, but
> completely forgot about it. Thanks! I'll get back with you and let
> you know how things work out.
>
> Frank

Hi all,

I changed things around and am now using the array version. Everything
for this part of the problem is working fine now. I still have other
issues with the program, but it's one step at a time, and I want to look
at it further myself and try to figure it out before posting here.
Thanks again!

Frank

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments