4

None of the posted answers I've read work, so I'm asking again.

I'm trying to copy the string data pointed to by a char pointer into a char array.

I have a function that reads from a ifstream into a char array

char* FileReader::getNextBytes(int numberOfBytes) {
    char *buf = new char[numberOfBytes];

    file.read(buf, numberOfBytes);

    return buf;
}

I then have a struct :

struct Packet {
    char data[MAX_DATA_SIZE]; // can hold file name or data
} packet;

I want to copy what is returned from getNextBytes(MAX_DATA_SIZE) into packet.data;

EDIT: Let me show you what I'm getting with all the answers gotten below (memcpy, strcpy, passing as parameter). I'm thinking the error comes from somewhere else. I'm reading a file as binary (it's a png). I'll loop while the fstream is good() and read from the fstream into the buf (which might be the data array). I want to see the length of what I've read :

cout << strlen(packet.data) << endl;

This returns different sizes every time: 8 529 60 46 358 66 156 After that, apparently there are no bytes left to read although the file is 13K + bytes long.

Gunslinger47
  • 7,001
  • 2
  • 21
  • 29
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 3
    or you could just pass into getNextBytes a pointer to packet.data... then you would put the data directly into it and avoid both the creation of an extraneous array of chars and the copying. – Justin Peel Mar 01 '12 at 06:32
  • 1
    what didn't work? did you try `strcpy` or `strncpy`? is your `buf` properly NULL terminated? – Naveen Mar 01 '12 at 06:32
  • I tried both of those and, yes, buf is properly NULL terminated. Buf might is actually binary data being read from fstream file – Sotirios Delimanolis Mar 01 '12 at 06:39
  • 1
    If you had to re post due to lack of responses or correct solutions, try awarding a Bounty on the question – Rohit Vipin Mathews Mar 01 '12 at 06:40

4 Answers4

4

This can be done using standard library function memcpy, which is declared in / :

strcpy(packet.data, buf);

This requires file.read returns proper char series that ends with '\0'. You might also want to ensure numberOfBytes is big enough to accommodate the whole string. Otherwise you could possibly get segmentation fault.

myin528
  • 522
  • 2
  • 7
3
//if buf not properly null terminated added a null char at the end
buf[numberofbytes] = "\0"

//copy the string from buf to struc
strcpy(packet.data, buf);

//or

strncpy(packet.data, buf);
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
2

Edit:
Whether or not this is being handled as a string is a very important distinction. In your question, you referred to it as a "string", which is what got us all confused.


Without any library assistance:

char result = reader.getNextBytes(MAX_DATA_SIZE);
for (int i = 0; i < MAX_DATA_SIZE; ++MAX_DATA_SIZE) {
  packet.data[i] = result[i];
}
delete [] result;

Using #include <cstring>:

memcpy(packet.data, result, MAX_DATA_SIZE);

Or for extra credit, rewrite getNextBytes so it has an output parameter:

char* FileReader::getNextBytes(int numberOfBytes, char* buf) {
  file.read(buf, numberOfBytes);

  return buf;
}

Then it's just:

reader.getNextBytes(MAX_DATA_SIZE, packet.data);

Edit 2:
To get the length of a file:

file.seekg (0, ios::end);
int length = file.tellg();
file.seekg (0, ios::beg);

And with that in hand...

char* buffer = new char[length];
file.read(buffer, length);

Now you have the entire file in buffer.

Gunslinger47
  • 7,001
  • 2
  • 21
  • 29
  • It all worked out in the end. But here you misunderstand the purpose of what I'm doing. If I could of course I would send the whole file in one packet. But we want to break it down and send as frames, which is why I'm passing the number of bytes to read (= to MAX_DATA_SIZE). But thanks. – Sotirios Delimanolis Mar 12 '12 at 20:20
0

strlen is not a valid way to determine the amount of binary data. strlen just reads until it finds '\0', nothing more. If you want to read a chunk of binary data, just use a std::vector, resize it to the amount of bytes you read from the file, and return it as value. Problem solved.

cooky451
  • 3,460
  • 1
  • 21
  • 39