Pretty much what it says in the title, _orgDrspath = std::string, newDrsName = std::string

std::cout << "Opening original " << _orgDrsPath << std::endl;
	// Store all bytes into a variable called exe
    std::ifstream inFileDrs(_orgDrsPath, std::ios::in | std::ios::binary);
	inFileDrs.seekg(0, std::ios::end);
	size_t len = inFileDrs.tellg();
	int newLen = 1024*1024;
	char *theDrs = new char[len];
	inFileDrs.seekg(0, std::ios::beg);
	inFileDrs.read(theDrs, len);
	char *newDrs = new char[newLen];

	std::ofstream outFileDrs(newDrsName, std::ios::out | std::ios::binary);
    std::cout << "Creating patched drs file " << newDrsName << std::endl;
	std::cout << "Patching DRS " << _orgDrsPath << std::endl;

    // Header consists of Text, then 0x1A EOF, then some 0x00's, then the version
    // The following code reads the whole header AND the first byte of the Version
    bool foundEof = false;
	char *byt = new char[1];
    while (true)
    {
		inFileDrs.read(byt, 1);
        outFileDrs.write(byt, 1);
		std::cout << int(byt) << std::endl;
        if (int(byt) == 0x1A)
		{
			std::cout << "Pass" << std::endl;
            foundEof = true;
		}
        else if (byt != '\0' && foundEof)
            break;
		delete [] byt;
		byt = new char[1];
    }

Recommended Answers

All 5 Replies

What is that loop beginning on line 20 trying to accomplish? Read a file one byte at a time until EOF? ifstream.read() will return NULL when EOF is reached, so all you need to do is this:

char byt; // no need for a pointer here
while( inFileDrs.read(&byt, 1) )
{

}

I am trying to read and write a file up until this point

else if (byt != '\0' && foundEof)

What is the significance of '\0'? That byte may appear in binary files hundreds or thousands of times, but will rarely, if ever, appear in text files.

I am reading a .drs extension file. First it needs to find this


if (int(byt) == 0x1A)

where the byt is == 0x1A

and then once it finds this byt i am looking for the first \0 character, then i can start processing the file.

Ok, well I am not sure why it wasn't working before, but I did what you suggested and it worked now :S Ty :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.