Read binary file to string, send as char array

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Read binary file to string, send as char array

Post 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.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

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

Post 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.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

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

Post 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.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

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

Post by hendu »

Stop using strings for binary data.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

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

Post by Foaly »

I can only agree with hendu.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

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

Post 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
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

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

Post by LunaRebirth »

Sorry that was a silly question. I'm sleep deprived. Anywho, I'll figure it out eventually. Thanks for the input.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

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

Post by Foaly »

You usually have to use char for binary data, as its size is one byte.
Just don't use the string class.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

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

Post 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
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply