471,831 Members | 1,029 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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];
string Temp;

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

Aug 27 '06 #1
3 10953
9,065 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
9,065 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 YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.