Figured this might be a nooby question that I'm just getting sick of Googling around for.
Basically I've made a file system that sends 10,000 characters at a time from the server to the client.
The client received these 10,000 characters, then determines if it creates a new file, overwrites the file, or adds onto the file.
This works wonders when sending text-based files (like a .txt document)
However, it doesn't work when sending binary files like a .png image.
I've tried reading a file's contents (with std::ios::binary) into a std::string.
If I save the std::string contents into a file, I get the duplicated file correctly YAY.
But since I have to convert the std::string to a char[10000], I believe it's being null-terminated and the char holds 26 characters (even though the output is only something like %PNG []).
So I'm trying to convert a binary std::string to a char[] array.
I've looked at this through memcpy, strcpy, even std::copy, and nothing seems to work right.
Suggestions would be helpful.
Read binary file to string, send as char array
-
- Posts: 386
- Joined: Sun May 11, 2014 12:13 am
-
- Posts: 386
- Joined: Sun May 11, 2014 12:13 am
Re: Read binary file to string, send as char array
+++
I've done this before on an early development project by doing this:
but now that I'm storing it into an std::string first (for comparisons and other reasons) and have to convert it to a char, I'm getting a null-terminating string of chars.
I've done this before on an early development project by doing this:
Code: Select all
FILE *file;
char *buffer;
unsigned long fileLen;
file = fopen(fileToSend, "rb");
/*...
...*/
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
buffer = new char[fileLen];
fread(buffer, fileLen, 1, file);
char size[MAX_PATH];
sprintf(size, "%i", fileLen);
//buffer now holds correct data without null-terminating!
fclose(file);
-
- Posts: 386
- Joined: Sun May 11, 2014 12:13 am
Re: Read binary file to string, send as char array
Hmm.. Maybe it isn't a null-terminator..?
I tried this for testing purposes:
and got no output from the printf.
I'm at a loss here
EDIT:
If I add onto the string by doing str+="TEST" with the file contents in it, the std::string saves correctly.
But again, when I convert to char for sending purposes, everything cuts off after "PNG% []" (including "TEST" at the end).
It's like it's a null-terminator that I can't seem to fix.
I tried this for testing purposes:
Code: Select all
std::string changeNullTerms(std::string str) {
std::string newStr = "";
for (int x = 0; x < str.length(); x++) {
if (str.at(x) == '\0') {
newStr += str.substr(0,x-1);
newStr += "//0//";
newStr += str.substr(x+1,str.length()-x-1);
printf("/tReplaced a null-term!\n");
}
}
if (newStr == "")
return str;
return newStr;
}
I'm at a loss here
EDIT:
If I add onto the string by doing str+="TEST" with the file contents in it, the std::string saves correctly.
But again, when I convert to char for sending purposes, everything cuts off after "PNG% []" (including "TEST" at the end).
It's like it's a null-terminator that I can't seem to fix.
Re: Read binary file to string, send as char array
Stop using strings for binary data.
Re: Read binary file to string, send as char array
I can only agree with hendu.
-
- Posts: 386
- Joined: Sun May 11, 2014 12:13 am
Re: Read binary file to string, send as char array
Can char arrays hold binary data?
Otherwise what else can I use?
Need for send() and recv() so char[] is really my only option
Otherwise what else can I use?
Need for send() and recv() so char[] is really my only option
-
- Posts: 386
- Joined: Sun May 11, 2014 12:13 am
Re: Read binary file to string, send as char array
Sorry that was a silly question. I'm sleep deprived. Anywho, I'll figure it out eventually. Thanks for the input.
Re: Read binary file to string, send as char array
You usually have to use char for binary data, as its size is one byte.
Just don't use the string class.
Just don't use the string class.
Re: Read binary file to string, send as char array
As people is saying, work with raw chars. std::string is for text strings (thus everything goes well with text files), so, just send your data as 10000 chars, and when you're sending text, make sure you send a null terminated stream of bytes (i.e. you want to send a string? send the string, and following act, send a 0) std::string expects null terminated arrays of chars
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt