473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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];
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 11063
Banfa
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
Banfa
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

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

Similar topics

10
by: David Rasmussen | last post by:
If I have this struct S { int arr; }; and I do this: S s1,s2;
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
4
by: Lafer | last post by:
Hello, I am attempting to write a terminal interface program using PowerPC Assembly/C, where I am using an integer-to-ASCII conversion algorithm. Unfortunately, I have run into some difficulties...
10
by: Cocy | last post by:
Hi, This might be a sort of FAQ, but I don't see why, so I would someone help me to understand what's wrong? I've just created following code which wold trim white space(s) in a (given) string....
6
by: Marcelo | last post by:
Hi, I have a declaration like the following: char tmp; char tmp2; strcpy (tmp, "58257619"); strcpy (tmp2, "");
5
by: Frederick Gotham | last post by:
I want to copy an array of objects. If it were C, I'd simply do: memcpy( target_array, source_array, sizeof target_array ); However, this won't suffice for objects in C++.
1
by: Flemming Hansen | last post by:
Hi guys, I want to load a binary file in to an array by splitting the file in 8 bit pieces. I mean: FileStream s = new FileStream(path, FileMode.OpenOrCreate); // Open StreamReader r = new...
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
5
by: Ramesh | last post by:
Hi. Assuming I have a code snippet like below: #include <iostream> using namespace std; char Mac = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.