Read and write binary?

I've only recently started to work in C++, so I'm not quite adept to it yet. What I was trying to do is to make is read bytes from multiple files and write them to new file in their respective order, and also write locations and length of each file into another file. I don't even know how to start here, if I used .NET to do this it wouldn't be a problem for me, but in C++ as I mentioned, I am a newb to it. What I'm asking for is someone to give me pointers or any kind of help so I can accomplish this.
The algorithm can't be any different than what you'd use for a .NET implementation. Post your .NET version and someone can help you port it.
hello again, sorry for late response, anyway i've made the first part of this, but now im having trouble getting those resources from the file.

this is what i used;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        std::string rdr = defaultLocation;
        rdr.append("files.ini");
        INIReader reader(rdr);
        if (reader.ParseError() < 0) {
            std::cout << "Can't load 'files.ini'!" << std::endl;
            return S;
        }
        std::string x1 = "p_";
        std::string x2 = "s_";
        std::string x3 = "l_";
        x1.append(namex);
        x2.append(namex);
        x3.append(namex);
        std::string Pack = defaultLocation;
        Pack.append(reader.Get("", x1.c_str(), "null"));
        std::string Start = reader.Get("", x2.c_str(), "null");
        std::string Length = reader.Get("", x3.c_str(), "null");
        unsigned long long StartI = ll_from_string(Start);
        unsigned long long LengthI = ll_from_string(Length);
        char * data = new char [LengthI];
        std::ifstream myfile;
        myfile.open(Pack.c_str());
        myfile.seekg(StartI);
        myfile.read(data,LengthI);
        myfile.close();
        std::ofstream myfile2;
        myfile2.open(namex.c_str());
        myfile2.write(data, LengthI);
        myfile2.close();
        //luaL_loadbuffer(S, data, LengthI, namex.c_str()); 

it exports the file, but it is not the same as the one i inputted inside, the size appears to be bigger by few hundred bytes, but the beggining and the ending bytes i looked at with hex editor appear to be same for the both files, still if the file is image, the input file looks normal, while the output one is a junk of pink pixels

ALSO not to create confusion, this is the ini file:
p_Sun_Flower.jpg=c.pack
s_Sun_Flower.jpg=0
l_Sun_Flower.jpg=275854
p_core.lua=c.pack
s_core.lua=275854
l_core.lua=115
p_Maples.jpg=c.pack
s_Maples.jpg=275969
l_Maples.jpg=379128

p_ = package file
s_ = address/location in file
l_ = length in bytes (seems to be correct when compared to original file)
Last edited on
Mmm, not sure what's going on. The thing to do is print the values of StartI and LengthI. Check the file size is LengthI. Compare the bytes in the source file starting at StartI.

I've rearranged your code to make it more readable (to me). I've removed close() calls, used an smart pointer to manage the dynamic buffer, and truncated the file before use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
std::string rdr = defaultLocation + "files.ini";
INIReader reader(rdr);
if (reader.ParseError() < 0) {
	std::cout << "Can't load 'files.ini'!" << std::endl;
	return S;
}

std::string x1 = "p_" + namex;
std::string Pack = defaultLocation + reader.Get("", x1.c_str(), "null");

std::string x2 = "s_" + namex;
std::string Start = reader.Get("", x2.c_str(), "null");

std::string x3 = "l_" + namex;
std::string Length = reader.Get("", x3.c_str(), "null");

unsigned long long StartI = ll_from_string(Start);
unsigned long long LengthI = ll_from_string(Length);

std::unique_ptr<char> data(new char[LengthI]);

std::ifstream myfile(Pack);
myfile.seekg(StartI);
myfile.read(data.get(), LengthI);

std::ofstream myfile2(namex, std::ios::trunc);
myfile2.write(data.get(), LengthI);

//luaL_loadbuffer(S, data, LengthI, namex.c_str()); 
Topic archived. No new replies allowed.