468,497 Members | 2,026 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

Copying pieces of char array to string

Hi, I'm trying to write a class that will use ifstream to first extract a file's header into a char array, but then I want certain sections of that char array copied into a C++-style string (that is, the string class). Here's the code I have so far:

ifstream TheFile;
TheFile.open(OpenFileName,ios::in | ios::binary | ios::_Nocreate);
char *buffer = new char[512];
TheFile.read(buffer,512);
string Temp;

I would like to copy 4 characters from buffer[] starting at offset 0x100. How would I do this?

Thanks.
Aug 27 '06 #1
3 10836
Banfa
9,055 Expert Mod 8TB
char array[4];

memcpy(array, &buffer[0x100], 4);
Aug 27 '06 #2
Thanks for you reply, but I wanted to copy some text from the buffer[] array to the string Temp. Is there a way to do this, or should I first copy some stuff from buffer[] to another temporary char array, and then convert that char array to a string?
Aug 28 '06 #3
Banfa
9,055 Expert Mod 8TB
I think you need to copy to a tempary char array and then assign trhat to a string.

Don't forget to add a 0 terminator if it is a string that you are copying.
Aug 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by David Rasmussen | last post: by
5 posts views Thread by Roy Hills | last post: by
4 posts views Thread by Lafer | last post: by
5 posts views Thread by Frederick Gotham | last post: by
1 post views Thread by Flemming Hansen | last post: by
9 posts views Thread by Mr John FO Evans | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.