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

Strip String in 2 blocks

P: n/a
Hello,

I need separete a string in 2 blocks.
For example:
Which is my favourite fruit?|Orange
Which is my favourite color?|Blue
I try to use string.h functions, but always return problems.
My last tentative was:
if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}
The question is: How I can take the second period, with the answer??
Thanks!

Apr 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Senger wrote:
I need separete a string in 2 blocks.
For example:
Which is my favourite fruit?|Orange
Which is my favourite color?|Blue
I try to use string.h functions, but always return problems.
My last tentative was:
if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}
The question is: How I can take the second period, with the answer??


RTFM on strtok, if you're so attached to it. If not, use 'std::string',
and its members 'find' and 'substr'.

V
--
Please remove capital As from my address when replying by mail
Apr 11 '06 #2

P: n/a
int main()
{
char const str[] = "Which is my favourite fruit?|Orange";

That const shouldn't be there.

-Tomás
Apr 11 '06 #3

P: n/a
Hi Senger,
I need separete a string in 2 blocks.
For example:

Which is my favourite fruit?|Orange
Which is my favourite color?|Blue


you should also have a look at stringstreams: They
can be used to "cut" a string into pieces. Here is
an example:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::istringstream strm("first;second;third");
std::string str;
while(std::getline(strm, str, ';'))
std::cout << str << "\n";
}

And here is the output of this program:

first
second
third

Good luck,
Tilman
Apr 11 '06 #4

P: n/a
Great Solution!
But I still have a problem to convert the str to const char *:

std::istringstream strm(list);
std::string str;
int i=0;
while(std::getline(strm, str, '|')){
i++;
if (i==1){
m_HiddenList.InsertString(-1, str); //Problems... cannot convert
}else{
m_ListFiles.InsertString(-1,str); //Problems... cannot convert
}
}
I try to use a sprintf function, but without sucess...
Do you have any Idea??

Apr 17 '06 #5

P: n/a
Senger wrote:
Great Solution!
But I still have a problem to convert the str to const char *:

std::istringstream strm(list);
std::string str;
int i=0;
while(std::getline(strm, str, '|')){
i++;
if (i==1){
m_HiddenList.InsertString(-1, str); //Problems... cannot convert
}else{
m_ListFiles.InsertString(-1,str); //Problems... cannot convert
}
}
I try to use a sprintf function, but without sucess...
Do you have any Idea??


1. You should really use stringstream.
2. To convert your string to a const char * you can use str.c_str()

Apr 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.