Hello,
I´ve a problem with the follwing code lines:
string line;
ifstream myfile ("doorcoordinat es.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 6875
Martin Pöpping wrote: Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinat es.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******@despa mmed.com> wrote, Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinat es.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*******@rt f-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******@despa mmed.com> wrote,
Hello,
I´ve a problem with the follwing code lines:
string line; ifstream myfile ("doorcoordinat es.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, " ,.");)
{
}
} 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 convertStringToChar(string& str, char* data)
{
|
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. How can I
convert the sets of 4 bytes to floats?
Thanks,
Greg Book
|
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 function works properly. I also wrote another function to
| |
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() function. However, the .c_str() string class function
returns type const char*.
The relevant code may be found below. My compiler returns the
error "invalid conversion from `const char*' to `char*'".
|
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 short
*)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
+2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
+8))),htons(*((unsigned short...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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.
| |