Hello,
I´ve a problem with the follwing code lines:
string line;
ifstream myfile ("doorcoordinates.txt");
if (myfile.is_open())
{
char str[100];
char * pch;
getline (myfile,line);
str=line;
pch = strtok (str,";");
while (pch != NULL)
{
pch = strtok (NULL, " ,.");
}
myfile.close();
}
line:
str=line;
is the problem, because the compiler says, that he cannot convert from
std::str to char[100].
How do I convert/cast from std::str to char[100]?
Thanks a lot for help,
Martin 18 6859
Martin Pöpping wrote: Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinates.txt"); if (myfile.is_open()) { char str[100]; char * pch; getline (myfile,line); str=line; pch = strtok (str,";"); while (pch != NULL) { pch = strtok (NULL, " ,."); } myfile.close(); }
line: str=line;
is the problem, because the compiler says, that he cannot convert from std::str to char[100].
How do I convert/cast from std::str to char[100]?
strtok takes a const char *, so don't set up all the temporary
variables, just do this:
pch = strtok (line.c_str(),";");
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
>... line: str=line; ... is the problem, because the compiler says, that he cannot convert from std::str to char[100].
sprintf(str, "%s", line.c_str());
Maybe it's little wrong for "plain" C++,
but it works.
Ben Pope wrote: strtok takes a const char *,
No, it doesn't.
so don't set up all the temporary variables, just do this:
pch = strtok (line.c_str(),";");
This shouldn't compile.
Rolf Magnus wrote: strtok takes a const char *,
No, it doesn't.
so don't set up all the temporary variables, just do this:
pch = strtok (line.c_str(),";");
This shouldn't compile.
Yes you´re right. The error message is now:
'strtok' : cannot convert parameter 1 from 'const char *' to 'char *'
On Sun, 08 Jan 2006 16:32:48 +0100 in comp.lang.c++, Martin Pöpping
<ma******@despammed.com> wrote, Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinates.txt"); if (myfile.is_open()) { char str[100];
Yes, your problem is the char[100]. You forgot about using
std::string throughout and fall back to char arrays. Where the heck
did that magic 100 come from anyway? How do you know that the
"correct" value is not 98 or 101 instead?
Instead, look for an approach along the lines of: http://groups.google.com/gr*********....earthlink.net
>Yes, your problem is the char[100]. You forgot about using std::string throughout
I should have added, your code is easily patched up by changing
str=line;
to
strcpy(str, line.c_str());
but that would be a bad way to go for the reasons given.
Rolf Magnus wrote: Ben Pope wrote:
strtok takes a const char *,
No, it doesn't.
Correct. so don't set up all the temporary variables, just do this:
pch = strtok (line.c_str(),";");
This shouldn't compile.
Correct.
My bad for not trying it, and instead relying on the following reference: http://www.cplusplus.com/ref/cstring/strtok.html
The text does describe that it modifies the string.
Apologies.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
On Sun, 8 Jan 2006 18:26:02 +0200 in comp.lang.c++, "Marchello"
<ma*******@rtf-15.ntu-kpi.kiev.ua> wrote, sprintf(str, "%s", line.c_str());
Oh, too bad you pay the cost for the heavier sprintf instead of the
lightweight strcpy, and then forget to take advantage of the one
benefit it gives you:
sprintf(str, "%99s", line.c_str());
David Harmon wrote: On Sun, 08 Jan 2006 16:32:48 +0100 in comp.lang.c++, Martin Pöpping <ma******@despammed.com> wrote,
Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinates.txt"); if (myfile.is_open()) { char str[100];
Yes, your problem is the char[100]. You forgot about using std::string throughout and fall back to char arrays. Where the heck did that magic 100 come from anyway? How do you know that the "correct" value is not 98 or 101 instead?
Instead, look for an approach along the lines of: http://groups.google.com/gr*********....earthlink.net
even better than using char[100], which is a magic number, use a
vector<char>:
while (getline(myfile,line))
{
vector<char> buf(line.begin(), len.end());
buf.push_back('\0');
for (char *pch = strtok(&buf[0],";");
pch = strtok(0, " ,.");)
{
}
}
In article <dp**********@newsreader3.netcologne.de>,
Martin Pöpping <ma******@despammed.com> wrote: Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinates.txt"); if (myfile.is_open()) { char str[100]; char * pch; getline (myfile,line); str=line; pch = strtok (str,";"); while (pch != NULL) { pch = strtok (NULL, " ,."); } myfile.close(); }
How about something like:
string::size_type begin = line.find( ';' );
if ( begin != string::npos ) do {
++begin;
string::size_type end = line.find_first_of( " ,.", begin );
// use line.substr( begin, end - begin ) for the subsection
begin = end;
}
while ( begin != string::npos );
Is there a better way of doing this without using strtok?
Martin Pöpping wrote: Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinates.txt");
If you really must use char* do this: if (myfile.is_open()) { char * str; char * pch; getline (myfile,line); str=new char[line.size()];
strcpy(str, line.c_str()); pch = strtok (str,";"); while (pch != NULL) { pch = strtok (NULL, " ,."); }
delete [] str; myfile.close(); }
line: str=line;
is the problem, because the compiler says, that he cannot convert from std::str to char[100].
How do I convert/cast from std::str to char[100]? Thanks a lot for help, Martin
Kindly do a google on c_str. go to the first link. you will get all
that you need.
hth
Moh
Kindly do a google on c_str. go to the first link. you will get all
that you need.
hth
Moh
Kindly do a google on c_str. go to the first link. you will get all
that you need.
hth
Moh
Kindly do a google on c_str. go to the first link. you will get all
that you need.
hth
Moh
Kindly do a google on c_str. go to the first link. you will get all
that you need.
hth
Moh
Marchello wrote: ... line: str=line; ... is the problem, because the compiler says, that he cannot convert from std::str to char[100].
sprintf(str, "%s", line.c_str());
Maybe it's little wrong for "plain" C++, but it works.
You should not use sprintf it is evil, use snprintf() :)
Cheers
Harley ro**********@gmail.com wrote: If you really must use char* do this: if (myfile.is_open()) { char * str; char * pch; getline (myfile,line); str=new char[line.size()]; strcpy(str, line.c_str()); pch = strtok (str,";"); while (pch != NULL) { pch = strtok (NULL, " ,."); } delete [] str; myfile.close(); }
If you really must use char* then ensure you allocate line.size() + 1.
It is highly unlikely you would want to use strtok though. Either write
a tokenising loop using find or find_first_of or use an opensource one
like boost::tokenizer if you don't want to write your own. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: karthik.naig |
last post by:
Hi,
This was the routine I wrote earlier to convert a C++ string to a
char array.
But I found that the char* array consisted only of junk after returning
from the below function.
|
by: Jim Langston |
last post by:
#include <string>
int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}
This works in Microsoft Visual C++ .net 2003
|
by: karthik.naig |
last post by:
Hi,
This was the routine I wrote earlier to convert a C++ string to a
char array.
But I found that the char* array consisted only of junk after returning
from the below function.
int...
|
by: Gregory.A.Book |
last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats....
|
by: sadegh |
last post by:
I wrote a function to convert any type to string:
template <typename T>
std::string toStr(const T &thing) {
std::ostringstream outStr;
outStr << thing;
return outStr.str();
}
the above...
|
by: Alan |
last post by:
How do I convert the type const char* to type char*?
I have an input string ("input_string") of class string. I need
to convert this to type char* in order to use it in a call to the
strtok()...
|
by: hamishd |
last post by:
Is this possible? Sorry if this question isn't relevant here.
actually, I'm really trying to convert a unsigned char * to an int
|
by: sam.barker0 |
last post by:
Hi guys,
I am trying to form an IPV6 address string from the address bytes
contained in a unsigned char buffer
char tempstring;
sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |