By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 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.

get(char*, num, delim) question

P: n/a
Hello,

I've got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here's where I'm doing something
incorrectly. This is what I've got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, '/');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*' to `char'"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.

The other tangent problem I'm having with this how to properly cycle
through the input file so it only reads in the first field and then
moves on to the next line. I tried
while (fin.good()) {
char first;
fin.get(first);
cout << first << endl;
}
return first;

Which looped, but it output the entire file. I only need the first
field. If the first field were only one character, then the only
problem I would be dealing with is the loop problem, but since it's
either 1 or 2 characters (with the end delimeter being '/'), I'm also
dealing with the get() problem.

Thanks for any advice.

Frank
Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
Hello,

I've got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here's where I'm doing something
incorrectly. This is what I've got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, '/');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*' to `char'"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.


Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, '/');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at
all. Just declaring a pointer does not make it point at anything. So in your
code you do not have any characters to read the first field into. Even if
you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated
pointers, just use an array.

Your second mistake was to use get instead of getline.

john
Jul 22 '05 #2

P: n/a
John Harrison wrote:
"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
Hello,

I've got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here's where I'm doing something
incorrectly. This is what I've got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, '/');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*' to `char'"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.

Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, '/');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at
all. Just declaring a pointer does not make it point at anything. So in your
code you do not have any characters to read the first field into. Even if
you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated
pointers, just use an array.

Your second mistake was to use get instead of getline.

john

Hi John,
First, thanks for the quick reply! Unfortunately, I have been
instructed to use the get() function vice the getline() function. From
what I understand about the get() function with the three parameters, it
reads characters into a buffer until num - 1 characters have been read,
or in my case, until the '/' delim character is encountered. This way,
if it reads a line where the first field is only one character, it reads
that character and then encounters the '/' delimeter and stops. But
it's the first parameter that I'm not doing correctly. ... However,
what you said about me having just declared a pointer and not pointing
to anything made me think of something. And I just tried something else
and it worked for both of my problems! This is what I did:

while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
delete [] first;

This displays 'sp' to the screen for that first line of text in the data
file and 'f' for the second line, and the first field for all of the
other lines until EOF.

So now I'm on to the next problem in my program. But I'll work on that
for a few hours before posting back if I need to. Thanks again John for
looking at this. Although I couldn't use the getline as you suggested,
your suggestion pointed me to my problem. Thanks!

Frank

Jul 22 '05 #3

P: n/a

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
John Harrison wrote:
"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
Hello,

I've got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here's where I'm doing something
incorrectly. This is what I've got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, '/');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*' to `char'"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.

Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, '/');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at all. Just declaring a pointer does not make it point at anything. So in your code you do not have any characters to read the first field into. Even if you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated pointers, just use an array.

Your second mistake was to use get instead of getline.

john

Hi John,
First, thanks for the quick reply! Unfortunately, I have been
instructed to use the get() function vice the getline() function.


Any particular reason?
From
what I understand about the get() function with the three parameters, it
reads characters into a buffer until num - 1 characters have been read,
or in my case, until the '/' delim character is encountered.
Yes, I'd forgotten about that, which is why I recommended getline,.but get
works too. The difference between get and getline, is that getline reads the
delimiter whereas get doesn't.
This way,
if it reads a line where the first field is only one character, it reads
that character and then encounters the '/' delimeter and stops. But
it's the first parameter that I'm not doing correctly. ... However,
what you said about me having just declared a pointer and not pointing
to anything made me think of something. And I just tried something else
and it worked for both of my problems! This is what I did:

I guess you missed out

char* first = new char[4];

or something similar.
while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
delete [] first;


Its still preferable to use an array instead of a pointer. Its more
efficient, safer and simpler, what's not to like?

Also you loop is wrong. The file might still be good even when you are at
the end of the file so you end up going round the loop one too many times.
Your loop should look like this

while (fin.get(first, 4, '/'))
{
cout << first << endl;
fin.ignore(80, '\n');
}

john
Jul 22 '05 #4

P: n/a
On Sat, 22 May 2004 22:54:45 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
John Harrison wrote:
> "Francis Bell" <ph**********@charter.net> wrote in message
> news:10*************@corp.supernews.com...
>
>>Hello,
>>
>>I've got a program that is reading in a data file of 25 lines. Here is
>>an example of the first two lines:
>>
>>sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
>>f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0
>>
>>In my program, I need to read in each line, BUT, I need to build a
>>different object based upon the characters in the first field. So, I
>>need to GET the first characters. And here's where I'm doing something
>>incorrectly. This is what I've got so far:
>>
>>char readInFirstChars(ifstream &fin) {
>>char *first;
>> fin.get(first, 3, '/');
>> cout << first; // debugging line to see what is output
>>}
>>return first;
>>}
>>
>>The above code give me a compile error stating:
>>" invalid conversion from `char*' to `char'"
>>
>>I tried the get function with the single character parameter like such:
>> char first;
>> fin.get(first);
>> cout << first << endl;
>> return first;
>>
>>and it works fine. So I must be doing something wrong with the pointer.
>
>
> Yes, two different things actually. And of course you should be using a
> pointer at all. Try this
>
> char first[3];
> fin.getline(first, 3, '/');
>
> Important lesson is to understand the difference between
>
> char *first;
>
> and
>
> char first[3];
>
> The first declares a pointer to char, it does not create any charactersat > all. Just declaring a pointer does not make it point at anything. So inyour > code you do not have any characters to read the first field into. Evenif > you had got it to compile it would have crashed when you tried to do the
> read.
>
> The second declares an array of three characters, these are the three
> characters you are going to read your field into. No need forcompilcated > pointers, just use an array.
>
> Your second mistake was to use get instead of getline.
>
> john
>
>

Hi John,
First, thanks for the quick reply! Unfortunately, I have been
instructed to use the get() function vice the getline() function.


Any particular reason?
From
what I understand about the get() function with the three parameters, it
reads characters into a buffer until num - 1 characters have been read,
or in my case, until the '/' delim character is encountered.


Yes, I'd forgotten about that, which is why I recommended getline,.but get
works too. The difference between get and getline, is that getline reads the
delimiter whereas get doesn't.
This way,
if it reads a line where the first field is only one character, it reads
that character and then encounters the '/' delimeter and stops. But
it's the first parameter that I'm not doing correctly. ... However,
what you said about me having just declared a pointer and not pointing
to anything made me think of something. And I just tried something else
and it worked for both of my problems! This is what I did:


I guess you missed out

char* first = new char[4];

or something similar.
while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
delete [] first;


Its still preferable to use an array instead of a pointer. Its more
efficient, safer and simpler, what's not to like?

Also you loop is wrong. The file might still be good even when you are at
the end of the file so you end up going round the loop one too many times.
Your loop should look like this

while (fin.get(first, 4, '/'))
{
cout << first << endl;
fin.ignore(80, '\n');
}

john


Yeah, sorry, I forgot to copy and paste the pointer declaration. What
you put is accurate. This project is for a class, and my lab
instructor said we needed to use get() because we need the practice
with it. Although an array may be safer and simpler, this form of
get() that I needed to use doesn't allow for an array in the
parameters, otherwise I would have used it (primarily because I'm just
now getting used to pointers and I know that I know just enough about
them to be dangerous: :) ) I'll use the loop enhancement. Thanks
again John!!

Frank
Jul 22 '05 #5

P: n/a
Francis Bell <ph**********@charter.net> 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
char first[3];
fin.getline(first, 3, '/');
while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
delete [] first;


How did you declare first? If as char first[4] then the call to
delete is unnecessary, and will, in some systems, cause a program
crash.
Jul 22 '05 #6

P: n/a
Siemel Naran wrote:
Francis Bell <ph**********@charter.net> 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
char first[3];
fin.getline(first, 3, '/');


while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
delete [] first;

How did you declare first? If as char first[4] then the call to
delete is unnecessary, and will, in some systems, cause a program
crash.

Hi all,

I declared first like this:

void readInFirstChars(ifstream &fin)
{
char* first;
first = new char[2];
while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
cout << "Here first" << endl;
if (first == "sp") {
cout << "Here second" << endl;
Spinnerbait temp;
readInASpinnerbait(fin);
temp.outputSpinnerbait(cout);
}
delete [] first;

I included my whole function because I spoke too soon earlier when I
said everything worked. My function is not getting inside the 'if'
clause. I know this because "Here first" is printing out to the screen,
but "Here second" is not. I'm pretty sure it's because of my condition
(first == "sp"). I know first is a pointer to my character array that
gets my first field from the data file, so comparing it with "sp" is
probably, no IS incorrect. But I tried dereferencing first and
comparing, and I got a compile error stating, "ISO C++ forbids
comparison between pointer and integer". So, that said, how do I go
about comparing these?

Jul 22 '05 #7

P: n/a
>
Yeah, sorry, I forgot to copy and paste the pointer declaration. What
you put is accurate. This project is for a class, and my lab
instructor said we needed to use get() because we need the practice
with it. Although an array may be safer and simpler, this form of
get() that I needed to use doesn't allow for an array in the
parameters,


That is not true, an array and a pointer are equivalent in this situation
(as they are in most situations).

john
Jul 22 '05 #8

P: n/a
> Hi all,

I declared first like this:

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

but as I said earlier you should be using an array

char first[4];

In either case though you need four characters, if you are going to say
fin.get(first, 4, '/');
while (fin.good())
{
fin.get(first, 4, '/');
cout << first << endl;
fin.ignore(80, '\n');
}
cout << "Here first" << endl;
if (first == "sp") {
This is wrong, you do not compare strings with ==

if (strcmp(first, "sp") == 0)
cout << "Here second" << endl;
Spinnerbait temp;
readInASpinnerbait(fin);
temp.outputSpinnerbait(cout);
}
delete [] first;
Get rid of this if you switch to an array.

I included my whole function because I spoke too soon earlier when I
said everything worked. My function is not getting inside the 'if'
clause. I know this because "Here first" is printing out to the screen,
but "Here second" is not. I'm pretty sure it's because of my condition
(first == "sp"). I know first is a pointer to my character array that
gets my first field from the data file, so comparing it with "sp" is
probably, no IS incorrect.
No, first == "sp" is a pointer comparison, it compares where first is
pointing to with where "sp" is. You need strcmp to copare the strings
themeselves.
But I tried dereferencing first and
comparing, and I got a compile error stating, "ISO C++ forbids
comparison between pointer and integer". So, that said, how do I go
about comparing these?


Seems like you are learning an old fashioned style of C++ programming.
Modern C++ has a string class which is easier to use than a char array (or
char pointer).

john
Jul 22 '05 #9

P: n/a
Francis Bell <ph**********@charter.net> 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;

Jul 22 '05 #10

P: n/a
On 22 May 2004 23:56:12 -0700, na*******@excite.com (Siemel Naran)
wrote:
Francis Bell <ph**********@charter.net> 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
Jul 22 '05 #11

P: n/a
> 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
Jul 22 '05 #12

P: n/a
Phrank wrote:
On 22 May 2004 23:56:12 -0700, na*******@excite.com (Siemel Naran)
wrote:

Francis Bell <ph**********@charter.net> 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

Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.