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 readInFirstChar s(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 12 2035
"Francis Bell" <ph**********@c harter.net> wrote in message
news:10******** *****@corp.supe rnews.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 readInFirstChar s(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(fir st, 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
John Harrison wrote: "Francis Bell" <ph**********@c harter.net> wrote in message news:10******** *****@corp.supe rnews.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 readInFirstChar s(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(fir st, 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
"Francis Bell" <ph**********@c harter.net> wrote in message
news:10******** *****@corp.supe rnews.com... John Harrison wrote: "Francis Bell" <ph**********@c harter.net> wrote in message news:10******** *****@corp.supe rnews.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 readInFirstChar s(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(fir st, 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
On Sat, 22 May 2004 22:54:45 +0100, "John Harrison"
<jo************ *@hotmail.com> wrote: "Francis Bell" <ph**********@c harter.net> wrote in message news:10******* ******@corp.sup ernews.com... John Harrison wrote: > "Francis Bell" <ph**********@c harter.net> wrote in message > news:10******** *****@corp.supe rnews.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 readInFirstChar s(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(fir st, 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
Francis Bell <ph**********@c harter.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(fir st, 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.
Siemel Naran wrote: Francis Bell <ph**********@c harter.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 readInFirstChar s(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;
readInASpinnerb ait(fin);
temp.outputSpin nerbait(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?
> 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
> Hi all, I declared first like this:
void readInFirstChar s(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; readInASpinnerb ait(fin); temp.outputSpin nerbait(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
Francis Bell <ph**********@c harter.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 readInFirstChar s(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; readInASpinnerb ait(fin); temp.outputSpin nerbait(cout); } delete [] first; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex Vinokur |
last post by:
I have several questins concerning a program below.
------ foo.cpp : BEGIN ------
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
|
by: kieran |
last post by:
Hi,
I'm using fstream.get to read in a character from a file, then print it
on the screen. I have a file called test.log that contains "Hello,
World!", but when I try and print the contents out on the screen I get
"Hello, World!!". The following is the code (please ignore all
wxWidgets classes):
void SystemBackupFrm::compress()
{
using namespace std;
|
by: HuYi |
last post by:
Hi,everyone,I want to get a string as '\u65e0\u6cd5' in TextBox,but when I
trace the program,I found that TextBox.Text=@"\u65e0\u6cd5",
But I want "\u65e0\u6cd5",not @"\u65e0\u6cd5".
I used TextBox.Text.ToArray(),the result:char chars =
{'\','u','6','5'....}.
I need char chars = {'\u65e0','\u6cd5'}
How should I do?Thanks.
|
by: Neo |
last post by:
how convert CString to char *?
regards,
Mohammad Omer Nasir
|
by: machikelxol |
last post by:
I'm having a strange error when I try reading from a file. here is the
code:
buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the
4th iteration.
This works fine until the fourth iteration.
breaks on this line: buffString = new char;
The contents of the text file it is reading:
1,1,1,1,1,1,1,1,1,5,6,7,8,9
2,2,2,2,2,2,2,2,2
3,3,3,3,3,3,3,3,3
| |
by: mailursubbu |
last post by:
Hi,
Will it be possible to #define a char pointer...
It means if write some thing like
#define CHAR_PTR (char *) // I know this wont work
I should be able to use CHAR_PTR to define a variable of type char *.
|
by: adamjroth |
last post by:
Hello,
I'm having trouble with IE's Range/Selection objects: when a user
click's anywhere in an element, can I create a range at that point
exactly _AND_ get the character offset? The code pasted below is a
very simple example that works in Firefox. I'd really like to know if
this can be reproduced in IE (knowing the char offset is very
important, as I need to store this information for later use).
<html>
|
by: veblen.lee |
last post by:
for an example:
'a' value 0x61
'1' value 0x31.
|
by: shaif |
last post by:
Hi i have problem with separating Character from string,
e.g:
dim S as string
dim k as string
S="23 * 12 UBV 100";
how to i get output k="UBV"
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |