Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with std::string to char[128]

Reply
Thread Tools

Help with std::string to char[128]

 
 
SteelSide@gmail.com
Guest
Posts: n/a
 
      09-09-2006
Ive searched and searched,but havent been able to get it to work.


-----------------
#include <iostream>

using namespace std;

std::string tempHostNameStr = "s1e2.hidden.thingy.org";
char szHostName[128];

for(int i=0; i < std::string tempHostNameStr.size(); i++){
szHostName[i] = tempHostNameStr[i];}


Errors:
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected unqualified-id before "for"
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected `,' or `;' before "for"
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected constructor, destructor, or type conversion before '<'
token
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected `,' or `;' before '<' token
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected constructor, destructor, or type conversion before '++'
token
C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
error: expected `,' or `;' before '++' token

Execution terminated

-----------------
Ive also tried using .c_str(),and assigning it directly by szHostName =
tempHostNameStr;

Anyone got a suggestion?

 
Reply With Quote
 
 
 
 
Markus Grueneis
Guest
Posts: n/a
 
      09-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> Ive searched and searched,but havent been able to get it to work.
>
> [snipped: incomplete program]
>
> Errors:
> C:\Documents and Settings\Daniel\Mina dokument\c++\stringtest.cpp:13:
> error: expected unqualified-id before "for"
>
> [snipped: error messages, all about the same origin]
> Execution terminated
>
> -----------------
> Ive also tried using .c_str(),and assigning it directly by szHostName =
> tempHostNameStr;
>


This is course couldn't work. Either use strncpy(), or the for-loop you
tried (but see comments below).

> Anyone got a suggestion?
>


Did you
#include <string>
?

It would have been nice to see the minimal, ought-to-work program. This
is it not, as there is no main() etc. etc. I just can guess if you just
forgot to post your #include directive or not.

Additionally, about such for-loops:

* use size_t for i, if you compare against a size_t. Then the compiler
will also not complain about comparison of signed with unsigned
values.
* check if your destination buffer is big enough for your string! And
no, "but this string will only have max. 60 chars!" is no argument.
Even for trivial programs, as you will import such a lazy style into
your more serious work (or into your exam, where it will have a bad
impact on your grade).

Finally: Why do you want to do this? As an inexperienced programmer you
could not care less about traditional C-style arrays and strings, as the
C++ classes are there for a reason: they make life easier, and more
safe. If you write about your problem, we maybe can point out a better
solution in the first place.


best regards,
-- Markus

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-09-2006
(E-Mail Removed) wrote:
>
> for(int i=0; i < std::string tempHostNameStr.size(); i++){


Get rid of the std::string. You'll also have to stick a terminating null
character after the characters that you copy.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-10-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Ive searched and searched,but havent been able to get it to work.
>
>
> -----------------
> #include <iostream>
>
> using namespace std;
>
> std::string tempHostNameStr = "s1e2.hidden.thingy.org";
> char szHostName[128];
>
> for(int i=0; i < std::string tempHostNameStr.size(); i++){
> szHostName[i] = tempHostNameStr[i];}


for ( int i = 0; i < tempHostNameStr.size(); ++i )

get rid of the std::string here. But you might as well use strcpy.

std::string tempHostNameStr = "s1e2.hidden.thingy.org";
char szHostName[128];
if ( tempHostNameStr.size() > 127 )
{
std::cout << "Evil! Host name too long!" << std::endl;
return 0;
}
else
strcpy( szHostName, tempHostNameStr.c_str() );

It looks like you are doing this copy, however, because you are using a call
that wants a char* and you have a std::string. std::string has the c_str()
that returns a pointer to a c-style string (null terminated char array). It
is const, however, meaning you can't change it.

SomeNetworkFunction( tempHostNameStr.c_str() );

A lot of times some calls won't be const correct. That is, they will ask
for a char* although they don't change the contents and really should be
const char*. In these cases it is *usually* safe to do a const_cast to
throw away the constantness of the c_str() but you have to be 1000% sure
that the function won't try to change the string.

SomeNetworkFunction( cost_cast<char*>( tempHostNameStr.c_str() );

HTH

Also, of course, remember to
#include <string>


 
Reply With Quote
 
SteelSide@gmail.com
Guest
Posts: n/a
 
      09-11-2006
Thanks for all your answers I appriciate your fast respond time i
decided to go for the forloop with a \0 at the end.
I dont know what was wrong,but why do you want me to include string?
dosn't iostream contain it? Im very sorry but im just a starter at c++

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      09-11-2006

(E-Mail Removed) wrote:
> Thanks for all your answers I appriciate your fast respond time i
> decided to go for the forloop with a \0 at the end.
> I dont know what was wrong,but why do you want me to include string?
> dosn't iostream contain it? Im very sorry but im just a starter at c++


Standard headers are permitted to include other standard headers, but
there are no requirements that any particular standard header includes
any other particular standard header.

If you rely on the fact that in your particular standard library
implementation, header <abc> includes header <xyz>, then you might find
your code does not compile when you change to a different compiler or
standard library implementation. To avoid this problem, whenever you
use a standard library component, always explicitly include the header
that declares it. std::cout is declared in <iostream>. If you use
std::cout, #include <iostream>. std::string is declared in <string>. If
you use std::string, #include <string>. That way, your code will always
work, regardless of what the standard library implementation you are
using that day does in terms of standard headers including other
standard headers.

As a side issue, I'd be somewhat surprised if <iostream> included
<string>, though it is not forbidden. But anyway, as I said above, even
if it does that in your implementation, you should not rely on it.

Gavin Deane

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-11-2006
(E-Mail Removed) wrote:
> Thanks for all your answers I appriciate your fast respond time i
> decided to go for the forloop with a \0 at the end.
> I dont know what was wrong,but why do you want me to include string?
> dosn't iostream contain it? Im very sorry but im just a starter at c++
>


Each standard header is required to define certain names. Any reasonable
reference guide will tell you which names must be defined by each of the
standard headers. Standard headers are also allowed, but not required
to, define other names, since some other names are often needed in order
for the definitions of the required names to make sense. But having
found that some standard header defines a name that it's not required to
define, you cannot conclude that it also defines all the other names
defined in the header that's required to define that name. So, for
example, if <iostream> happens to provide the definition of
std::basic_string, you can't assume that it also provides the name
std::string.

In general, when you use any name from the standard library, include the
header that's required to define it.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments