473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Break ifstream input into words?

Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan

Oct 29 '06 #1
20 5090
dm*******@gmail.com wrote:
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this?
Input the words into strings, and then copy it to the character arrays, or
alllocate space for the pointers and copy to it. Or just pass the c_str ()
of the strings to the functions that takes constant c-style strings, if
appropriate.

--
Salu2
Oct 29 '06 #2

dmurra...@gmail.com wrote:
Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan
Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I'ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}

Oct 29 '06 #3
Thanks, I really appreciate it. I would love to use strings, and I
understand everyone knows it's the better way to go, however the
guidelines I was given to work with included using character arrays and
assigning pointers to them. Basically, the program I create needs to
gather a list of words from an input file and do an analysis of how and
when they appear. The current plan is to put these words into a linked
list, alphabetize them, and go from there. However, I'm stuck at
getting the words seperated. Once I can get them into char arrays, I
should be fine. It's just the breaking down of the file into words that
I'm having huge problems with. I'm guessing that if I go with the
character array, the best way to do this will be to go through the file
character by character, looking for spaces and punctuation and putting
words into their nodes in a linked list. However, I don't know how to
do this - I'm not familiar with how to look at each character in a
file, and then how to create a character array ("word") with the data.

Again, thanks for all your help, hopefully this makes it a little more
clear what I'm going for!

Dan
Salt_Peter wrote:
dmurra...@gmail.com wrote:
Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan

Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I'ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}
Oct 29 '06 #4
dm*******@gmail.com wrote:
Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan
Dan - what don't you get, it's simple. (massive tongue in cheek here).

Hope this helps.

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

const char * GetStr( std::string & i_str )
{
return i_str.c_str();
}

int main()
{
std::string str;
std::vector<std::string vec;

while ( std::cin >str )
{
vec.push_back( str );
}

std::vector<const char * vec2( vec.size() );

std::transform( vec.begin(), vec.end(), vec2.begin(), GetStr );

const char ** array = & vec2[0];
const int num_array = vec2.size();

// stuff is in array - don't touch vec *or* vec2 while you use
// "array" otherwise you could be using dangling pointers

for ( int i = 0; i < num_array; ++ i )
{
std::cout << array[i] << "\n";
}

}
Oct 29 '06 #5
>dmurra...@gmail.com wrote:
Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan

Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I'ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}
"dmurray14" <dm*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks, I really appreciate it. I would love to use strings, and I
understand everyone knows it's the better way to go, however the
guidelines I was given to work with included using character arrays and
assigning pointers to them. Basically, the program I create needs to
gather a list of words from an input file and do an analysis of how and
when they appear. The current plan is to put these words into a linked
list, alphabetize them, and go from there. However, I'm stuck at
getting the words seperated. Once I can get them into char arrays, I
should be fine. It's just the breaking down of the file into words that
I'm having huge problems with. I'm guessing that if I go with the
character array, the best way to do this will be to go through the file
character by character, looking for spaces and punctuation and putting
words into their nodes in a linked list. However, I don't know how to
do this - I'm not familiar with how to look at each character in a
file, and then how to create a character array ("word") with the data.

Again, thanks for all your help, hopefully this makes it a little more
clear what I'm going for!

Dan
Salt_Peter wrote:
If you really have to, just read each word into a std::string, then copy it
to a c-string.

std::string MyString;
MyISteam >MyString;
That will get one "word", although when it hits the end of line it won't
read anymore.
One way to deal with this is to read a line at a time, put that into a
stringstream, then read the words out.

std::string Line;
while ( std::getline( MyIStream, Line ) )
{
std::stringstream LineStream;
LineStream << Line;
std::string Word;
while ( LineStream >Word )
{
// Do something with the word contained in the std::string here.
Since you need CStyle strings...
char Word[100];
strcpy( Word, Word.c_str() );
// Now Word has the word in it, what do you want to do with it?
}
}
Oct 29 '06 #6
<dm*******@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!
Here is an example using strings to split out the words. C++ has some nice
features to make it easy. If you really need char * after then you can
convert them once you have the separate word. Otherwise there is lots of
checking involved. If you really want to use char * have a look at the
standard library function strtok()

Otherwise this works quite well
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

void split(std::string &line, const std::string &separators,
std::vector<std::string&words);

int main()
{
std::ifstream in("file.txt");
std::string line;
std::vector<std::stringword_list;
const std::string word_separators(" ,.;:?!");

while(in && getline(in, line))
{
split(line, word_separators, word_list);
}

for(std::vector<std::string>::const_iterator i=word_list.begin();
i!=word_list.end(); i++)
{
std::cout << "Word: [" << *i << "]" << std::endl;
}
}

void split(std::string &line, const std::string &separators,
std::vector<std::string&words)
{
int n = line.length();
int start, stop;
start = line.find_first_not_of(separators);
while((start >= 0) && (start < n))
{
stop=line.find_first_of(separators, start);
if((stop < 0) || (stop n))
{
stop = n;
}
words.push_back(line.substr(start, stop - start));
start=line.find_first_not_of(separators, stop+1);
}
}

Oct 29 '06 #7
BTW - someone else is going to tell you to stop ""TOP POSTING"". Not
me, I don't mind but others will, be ready !

dmurray14 wrote:
Thanks, I really appreciate it. I would love to use strings, and I
understand everyone knows it's the better way to go, however the
guidelines I was given to work with included using character arrays and
assigning pointers to them.
That sentence makes no sense - you can't assign pointers to a character
array. It might sound like I'm being pedant but it seems like there may
be some confusion so you need to ask.
.... Basically, the program I create needs to
gather a list of words from an input file and do an analysis of how and
when they appear. The current plan is to put these words into a linked
list, alphabetize them, and go from there. However, I'm stuck at
a) why linked list ? Why not insert them directly into a std::map ?
b) exactly what information are you looking for ? Positional information
as well (like line number? location in file ? etc ..)
getting the words seperated. Once I can get them into char arrays, I
should be fine. It's just the breaking down of the file into words that
I'm having huge problems with. I'm guessing that if I go with the
character array, the best way to do this will be to go through the file
character by character, looking for spaces and punctuation and putting
words into their nodes in a linked list. However, I don't know how to
do this - I'm not familiar with how to look at each character in a
file, and then how to create a character array ("word") with the data.
std::istream "knows" how to do this when you read into a std::string.
>
Again, thanks for all your help, hopefully this makes it a little more
clear what I'm going for!

Dan
Salt_Peter wrote:
>dmurra...@gmail.com wrote:
>>Hey guys,

I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Dan
Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I'ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}
Oct 29 '06 #8
Jim Langston wrote:
....
// Do something with the word contained in the std::string here.
Since you need CStyle strings...
char Word[100];
****** WARNING ******* - Security hole - right here!
strcpy( Word, Word.c_str() );
It can't be stressed enough. Don't do an unbounded copy into any array
variable, especially one on the stack and even more especially from user
input. You will be 0wn3d.

Never use "strcpy" or "sprintf" or the like.
Oct 29 '06 #9
Wow, you guys are a huge help! Seriously, this is great.. I really
appreciate it. Let me respond to Gianni's post:

Gianni Mariani wrote:
BTW - someone else is going to tell you to stop ""TOP POSTING"". Not
me, I don't mind but others will, be ready !
I don't know what "TOP POSTING" is, so sorry if I did something wrong.
>
That sentence makes no sense - you can't assign pointers to a character
array. It might sound like I'm being pedant but it seems like there may
be some confusion so you need to ask.
OK, sorry if it didn't make sense. Basically I need to store the words
in a character array inside a node of a linked list.
.... Basically, the program I create needs to
gather a list of words from an input file and do an analysis of how and
when they appear. The current plan is to put these words into a linked
list, alphabetize them, and go from there. However, I'm stuck at
a) why linked list ? Why not insert them directly into a std::map ?
b) exactly what information are you looking for ? Positional information
as well (like line number? location in file ? etc ..)
Those were the requirements for the assignment. After the words are
stored, I need to figure out how many times the words appear, which
should be something that I can handle. Just stuck on getting from the
ifstream down to words in a node, in a character array.

getting the words seperated. Once I can get them into char arrays, I
should be fine. It's just the breaking down of the file into words that
I'm having huge problems with. I'm guessing that if I go with the
character array, the best way to do this will be to go through the file
character by character, looking for spaces and punctuation and putting
words into their nodes in a linked list. However, I don't know how to
do this - I'm not familiar with how to look at each character in a
file, and then how to create a character array ("word") with the data.

std::istream "knows" how to do this when you read into a std::string.
Which is why I would love to be able to use strings, but again, that
wasn't the assignment. Probably on purpose, unfortunately.

Oct 30 '06 #10
dm*******@gmail.com wrote:
I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!
Based on the conversation so far, I get the impression that this is a
homework assignment, hence the limit on what you are allowed to use of
the language.

There are a couple of ways you can attack this problem depending on what
parts of the language you *can* use. For example, you can't use
std::string, but can you use std::vector? (A vector<charcan make a
handy string replacement.) Can you write your own classes? (You can hack
out a small string class of your own.)

Do this, write the program so it works on a text file that contains only
one word. Let me see what you end up with and I'll help you extend it
from there.

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #11

Gianni Mariani wrote:
>
strcpy( Word, Word.c_str() );

It can't be stressed enough. Don't do an unbounded copy into any array
variable, especially one on the stack and even more especially from user
input. You will be 0wn3d.

Never use "strcpy" or "sprintf" or the like.
Intesting. Assume for the moment that two processors ( one card with
two processors on it ) communicate with each other via a struct called
test. The struct test an area of memory called 'shared memory' that
both processors sees. Details aside, here's my ( ingoring the shared
memory business) current approach to this.

struct test {
char input1 [ max ];
char input2 [ max ];
char input3 [ max ];
char input4 [ max ];

};
# include <sstream>
int main()
{
double const velocity ( -44.222 ) ;
std::ostringstream oss;
oss << " SNR value is " << velocity << std::endl;
test t;
strcpy ( t.input1, oss.str().c_str() ) ;
std::cout << t.input1 << std::endl;
}

How would I copy the contents to input1 without the use of strcpy?
An aside:
In my application the code - on processor A - is akin to:

int const address_of_test ( 0x3F000000 );

test * ptr_test ( 0 );
ptr_test = ( test *)( address_of_test ) ;
double const velocity ( -44.222 ) ;
std::ostringstream oss;
oss << " SNR value is " << velocity << std::endl;
strcpy ( ptr_test->input1, oss.str().c_str() ) ;

NOTE: address_of_test is where the struct is created and both a and b
have access to said location. I'm thinking placement new would be
better here, but I need to do some more reading.

Oct 30 '06 #12
"dmurray14" <dm*******@gmail.comwrote:
Basically I need to store the words in a character array inside a
node of a linked list.
Do you need to write the linked list?

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #13

Daniel T. wrote:
dm*******@gmail.com wrote:
I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I'm having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I'm assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I'm used to strings.

Thanks a lot, much appreciated!

Based on the conversation so far, I get the impression that this is a
homework assignment, hence the limit on what you are allowed to use of
the language.

There are a couple of ways you can attack this problem depending on what
parts of the language you *can* use. For example, you can't use
std::string, but can you use std::vector? (A vector<charcan make a
handy string replacement.) Can you write your own classes? (You can hack
out a small string class of your own.)

Do this, write the program so it works on a text file that contains only
one word. Let me see what you end up with and I'll help you extend it
from there.

--
To send me email, put "sheltie" in the subject.
It is in fact work related to a class I'm taking, yes. So far, my plan
of attack is as follows: use getline to dump each line of the file into
a character array. Then, I'm going to run through the character array
looking for spaces, and pull out words every time I get to a space,
sending them into their own nodes. From there it should be easy to make
the framework to check to see whether the word has appeared before and
if so,just add to the count.

I don't think we're supposed to be making classes, no. The idea is
likely to stick to the basics so we get the concepts. Hopefully this
will work out...

Oct 30 '06 #14
"dmurray14" <dm*******@gmail.comwrote:
Daniel T. wrote:
dm*******@gmail.com wrote:

Do this, write the program so it works on a text file that contains only
one word. Let me see what you end up with and I'll help you extend it
from there.

It is in fact work related to a class I'm taking, yes. So far, my plan
of attack is as follows: use getline to dump each line of the file into
a character array. Then, I'm going to run through the character array
looking for spaces, and pull out words every time I get to a space,
sending them into their own nodes. From there it should be easy to make
the framework to check to see whether the word has appeared before and
if so,just add to the count.
OK, so start with a file that has only one word in it. You need to read
in the word and output what word it is and that it was used once. Can
you get that working?

If you do the above, that will (a) give me an idea of what you are
allowed to use of the language, and (b) give me an idea of how good you
are and therefore what you probably need help with.

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #15

dmurray14 wrote in message ...
>Wow, you guys are a huge help! Seriously, this is great.. I really
appreciate it. Let me respond to Gianni's post:
That part in your post was top-posting. The rest of that post was ok
"format".

We don't like to see the cart before the horse, the answer before the
question, etc..

Mr. Steinbach puts it:
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Another thing to mention:
Trim (delete) anything in a prior post that you are not responding to. Like:
if you are posting your newly corrected program, delete the old posted
program. We can simply refer to that post up-thread if we need to review it.
It wastes bandwith and needlessly takes up space on our hard drives.

These are not laws, just courtesy.

--
Bob R
POVrookie
Oct 30 '06 #16

ma740988 wrote in message ...
>
Gianni Mariani wrote:
>>
strcpy( Word, Word.c_str() );

It can't be stressed enough. Don't do an unbounded copy into any array
variable, especially one on the stack and even more especially from user
input. You will be 0wn3d.

Never use "strcpy" or "sprintf" or the like.

Intesting. Assume for the moment that two processors ( one card with
two processors on it ) communicate with each other via a struct called
test. The struct test an area of memory called 'shared memory' that
both processors sees. Details aside, here's my ( ingoring the shared
memory business) current approach to this.

struct test {
char input1 [ max ];
char input2 [ max ];
char input3 [ max ];
char input4 [ max ];
};
# include <sstream>
int main(){
double const velocity ( -44.222 ) ;
std::ostringstream oss;
oss << " SNR value is " << velocity << std::endl;
test t;
strcpy ( t.input1, oss.str().c_str() ) ;
std::cout << t.input1 << std::endl;
}

How would I copy the contents to input1 without the use of strcpy?
Use 'strncpy' or 'std::copy'.

--
Bob R
POVrookie
Oct 30 '06 #17

BobR wrote:
Use 'strncpy' or 'std::copy'.
Thanks...seems like it would work but I'm going crazy, it isn't.

I have it copying into a character array just fine. I can print it out
and everything is exactly as it should be. Now all that's left to do is
split the lines into words, and I JUST CAN'T figure it out for the life
of me! I've tried strcmp, it tells me it needs Char* (which I thought
was what I was giving it, but apparently no good), and I've tried even
doing something like this, which doesn't work:

//cp = "Where in the world is carmen sandiego"

char char1[] = "W";
char char2[] = cp[0];

strcmp(char1, char2);

Even that won't work. I can't even get it to compare the first letter,
let alone the whole word! I am now completely lost as to how to break
these strings apart into words. Please help!

Dan

Oct 30 '06 #18
"dmurray14" <dm*******@gmail.comwrote:
BobR wrote:
Use 'strncpy' or 'std::copy'.

Thanks...seems like it would work but I'm going crazy, it isn't.

I have it copying into a character array just fine. I can print it out
and everything is exactly as it should be. Now all that's left to do is
split the lines into words, and I JUST CAN'T figure it out for the life
of me! I've tried strcmp, it tells me it needs Char* (which I thought
was what I was giving it, but apparently no good), and I've tried even
doing something like this, which doesn't work:

//cp = "Where in the world is carmen sandiego"

char char1[] = "W";
char char2[] = cp[0];

strcmp(char1, char2);

Even that won't work. I can't even get it to compare the first letter,
let alone the whole word! I am now completely lost as to how to break
these strings apart into words. Please help!
You are having problems because you are trying to solve the problem the
wrong way. You need to take a "vertical slice" of the problem instead.
Get it to work when the file only has one word in it first, from
beginning to end, including loading the word in the linked list (was
that class provided by your teacher?) Show us the code and we can help
you from there.

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #19

dmurray14 wrote in message ...
>
//cp = "Where in the world is carmen sandiego"
char char1[] = "W";
char char2[] = cp[0];
strcmp(char1, char2);

Even that won't work. I can't even get it to compare the first letter,
let alone the whole word! I am now completely lost as to how to break
these strings apart into words. Please help!
Dan
You can compare two chars directly.

{
char cp[] = "Where in the world is carmen sandiego";
char char1 = 'W'; // note singlequote
if( cp[0] == char1 ){
std::cout <<" cp[0] == char1 "<<std::endl;
}
else{
std::cout <<" cp[0] != char1 "<<std::endl;
}
// out: cp[0] == char1

char cp2[] = "Where in the world is Carmen Sandiego";
if( strcmp( cp, cp2) == 0 ){
std::cout <<" cp == cp2 "<<std::endl;
}
else{
std::cout <<" cp != cp2 "<<std::endl;
}
// out: cp != cp2

int dif = std::strncmp( cp, cp2, 5);
std::cout <<" strncmp( cp, cp2, 5) ="<<dif<<std::endl;
// out: strcmp( cp, cp3, 5) =0
}

Some other things that may help you (in header <cctype>)
std::isalpha
std::isdigit
std::ispunct
std::isspace
std::tolower
std::toupper

--
Bob R
POVrookie
Oct 30 '06 #20
dmurray14 wrote:
It is in fact work related to a class I'm taking, yes. So far, my plan
of attack is as follows: use getline to dump each line of the file into
a character array. Then, I'm going to run through the character array
looking for spaces, and pull out words every time I get to a space,
sending them into their own nodes. From there it should be easy to make
the framework to check to see whether the word has appeared before and
if so,just add to the count.

I don't think we're supposed to be making classes, no. The idea is
likely to stick to the basics so we get the concepts. Hopefully this
will work out...
Well you could try using the standard function strtok or implement you own
version of it. There is enough info in other posts for you to get the idea.

I have attached an example using strtok for splitting the words out of lines.

But remember c-strings are just arrays of chars with a null at the end. Think of
them a bit like an array of ints with the last being a 0. You can access each
element of the array the same way you would with integers and compare them the
same way. Just be careful you do not go past the end of the array - nasty things
can happen.

#include <cstdio>
#include <iostream>
#include <cstring>

using std::FILE;
using std::fopen;
using std::fgets;
using std::endl;
using std::cout;

//create a maximum for the len of a line
#define MAX_LINE 10000

//Our list of separators we wish to split at
const char *separator=" ,.?!";

int main()
{
FILE *fp;

//open the file
fp=fopen("file.txt", "r");

//check its open
if(fp)
{
//check for end of file
while(!feof(fp))
{
//char array to store our line
char line[MAX_LINE];

//ptr to each word NB if you want to move the word somewhere you
//will need to allocate memory for it
char *ptr;

//read a line from the file no longer the MAX_LINE-1 or till
//the next new line
fgets(line, MAX_LINE, fp);

//Remove newline from fgets or it could be in the separator list
line[strlen(line)-1]='\0';

//get a pointer to the first word
ptr=strtok(line, separator);

//check if therer is a word on the line
if(ptr)
{
cout << "Word [" << ptr << "]" << endl;
//see if there are more words. NB strtok remember the input line
//so a NULL is passed to say we want to use the same line as the
//last call
while(ptr=strtok(NULL, separator))
{
cout << "Word [" << ptr << "]" << endl;
}
}
}
}

fclose(fp);

return 0;
}
Adrian
Oct 30 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
10
by: Aleks Dubinskiy | last post by:
Hi folks, I am well aware of the superiority of using streams and operators << / >> instead of the old-style printf and scanf. However, there is only one obstacle that prevents me from switching...
7
by: shawn | last post by:
Hi All, Am using MSVC6 compiler on a WinXP machine. Am trying to read a file using std::ofstream; the problem is that Tofstream.is_open() fails and Tifstream.rdstate() returns "2" which...
12
by: dough | last post by:
I want to read in lines from a file and then seperate the words so i can do a process on each of the words. Say the text file "readme.txt" contains the following: In the face of criticism from...
5
by: Dragonizer | last post by:
Hello all, I need a little help with this. I can't seem to get ifstream ( or fstream ) to read anything. In the code: ifstream input = NULL; input.open("tester.txt"); if(input.bad()) {...
4
by: Phil Latio | last post by:
I'm kind of looking for some guidance in respect of this obstacle I'm up against - please let me expand; I have an option group with three options (AND, OR, & NONE) and a text box which will...
0
by: arnuld | last post by:
any comments for improvement: /* C++ Primer - 4/e * * CHAPTER 10 - Associative Containers * * EXERCISE - 10.7 * Write a program to count and print the number of times each word *...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
21
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated dynamically. I have ran this program with a file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.