Page 1 of 1

Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 5:21 am
by LunaRebirth
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.

Re: Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 5:24 am
by LunaRebirth
+++

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);
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.

Re: Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 4:15 pm
by LunaRebirth
Hmm.. Maybe it isn't a null-terminator..?
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;
}
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.

Re: Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 5:31 pm
by hendu
Stop using strings for binary data.

Re: Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 9:26 pm
by Foaly
I can only agree with hendu.

Re: Read binary file to string, send as char array

Posted: Fri Aug 05, 2016 9:57 pm
by LunaRebirth
Can char arrays hold binary data?
Otherwise what else can I use?
Need for send() and recv() so char[] is really my only option

Re: Read binary file to string, send as char array

Posted: Sat Aug 06, 2016 5:44 am
by LunaRebirth
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

Posted: Sat Aug 06, 2016 6:06 am
by Foaly
You usually have to use char for binary data, as its size is one byte.
Just don't use the string class.

Re: Read binary file to string, send as char array

Posted: Sun Aug 07, 2016 11:57 am
by Mel
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