CPP gurus, please read!!

  • Thread starter Thread starter MatuX
  • Start date Start date
M

MatuX

Guest
Again, MatuX invades the board with boring programming questions ;-)

I'm still learning CPP (I started just 2 weeks ago), so, try to be nice...

I'm creating a MVE Player with GUI. What it has to do is to build an exe appending 2 files. One called w32stub.lib with a size of 310,784 and the MVE file (random size). It's just a front-end to don't have to use the harsh method of TeamX and a shortcut to don't have to download the 5mb program from the Descent group.

I'm using this simple method: Create an EXE, write w32stub.lib data in the EXE, and then write the MVE data in the EXE.

My problem is that the read and write functions aren't working correctly!

The final size of the exe is always some KBs bigger than what it should be.

I truncate the code to just the w32stub.lib creation part, to debug it. So, it will just copy w32stub.lib to afailed.exe (for example). The same as if you write in DOS: "copy w32stub.lib afailed.exe"

For my surprise, it does not even make the copy correctly!

Here is the code I'm using:

{
int hndStubFile, // Handle to the stub library (w32stub.lib)
hndMVEFile, // Handle to the MVE video
hndEXEFile; // Handle to the self-playing video

char *buf; // Buffer

unsigned int StubSize = _filelength(hndStubFile);
// Returns a correct file size

buf = (char *) malloc(StubSize);
// Allocate memory for the buffer

_read( hndStubFile, buf, StubSize);
// Read - returns 310,784 bytes readed

_write( hndEXEFile, buf, StubSize);
// Write - returns 310,784 bytes writen
}

The debug tells me that the bytes readed and writen by the functions is the correct, but, when I flush the memory (close the files and free the buffer), I found that the file size is 1051 bytes bigger.

All the files were opened as _O_RDONLY|_O_BINARY. And the EXE was created as _S_IWRITE. All the procedures are successful and the variables used (such as AppPath and strNewFName) are correct.
{
hndMVEFile = _open( strFileName, _O_RDONLY|_O_BINARY )
hndStubFile = _open( AppPath "\\w32stub.lib", _O_RDONLY|_O_BINARY )
hndEXEFile = _creat( AppPath strNewFName, _S_IWRITE )
}


Thanks in advance!



[p align=center]
MatuX
Co-Leader and Chief Programmer on
http://clanfusionn.hypermart.net/tmslogo1.gif
[font size=1]GFX by Smackrazor
[font size=2]http://www.modsquad.f2s.com[/p]
 
You could do manual byte-per-byte copying but that would be a waste of time.

How about using the good ol' DOS copy function? Your problem is that you're not specifying binary mode for combining the two files. Here's what you should be doing:

copy /b file1.in+file2.in+file3.in destination.out

-Xotor-

>Again, MatuX invades the board with
>boring programming questions ;-)
>
>I'm still learning CPP (I started
>just 2 weeks ago), so,
>try to be nice...
>
>I'm creating a MVE Player with
>GUI. What it has to
>do is to build an
>exe appending 2 files. One
>called w32stub.lib with a size
>of 310,784 and the MVE
>file (random size). It's just
>a front-end to don't have
>to use the harsh method
>of TeamX and a shortcut
>to don't have to download
>the 5mb program from the
>Descent group.
>
>I'm using this simple method: Create
>an EXE, write w32stub.lib data
>in the EXE, and then
>write the MVE data in
>the EXE.
>
>My problem is that the read
>and write functions aren't working
>correctly!
>
>The final size of the exe
>is always some KBs bigger
>than what it should be.
>
>
>I truncate the code to just
>the w32stub.lib creation part, to
>debug it. So, it will
>just copy w32stub.lib to afailed.exe
>(for example). The same as
>if you write in DOS:
>"copy w32stub.lib afailed.exe"
>
>For my surprise, it does not
>even make the copy correctly!
>
>
>Here is the code I'm using:
>
>
>{
> int hndStubFile, // Handle
>to the stub library (w32stub.lib)
>
> hndMVEFile,
> // Handle to
>the MVE video
> hndEXEFile;
> // Handle to
>the self-playing video
>
> char *buf;
>// Buffer
>
> unsigned int StubSize = _filelength(hndStubFile);
>
> // Returns a correct
>file size
>
> buf = (char *) malloc(StubSize);
>
> // Allocate memory for
>the buffer
>
> _read( hndStubFile, buf, StubSize);
> // Read - returns
>310,784 bytes readed
>
> _write( hndEXEFile, buf, StubSize);
> // Write - returns
>310,784 bytes writen
>}
>
>The debug tells me that the
>bytes readed and writen by
>the functions is the correct,
>but, when I flush the
>memory (close the files and
>free the buffer), I found
>that the file size is
>1051 bytes bigger.
>
>All the files were opened as
>_O_RDONLY|_O_BINARY. And the EXE was
>created as _S_IWRITE. All the
>procedures are successful and the
>variables used (such as AppPath
>and strNewFName) are correct.
>{
> hndMVEFile = _open( strFileName, _O_RDONLY|_O_BINARY
>)
> hndStubFile = _open( AppPath
> "\\w32stub.lib", _O_RDONLY|_O_BINARY )
> hndEXEFile = _creat( AppPath
> strNewFName, _S_IWRITE )
>}
>
>
>Thanks in advance!
>
>

>[p align=center]
>MatuX
>Co-Leader and Chief Programmer on
>http://clanfusionn.hypermart.net/tmslogo1.gif
>[font size=1]GFX by Smackrazor
>[font size=2]http://www.modsquad.f2s.com[/p]


[div align=center]

http://www.poseidonet.f2s.com/files/nostupid.gif
[/div]
 
Hmmm??

>You could do manual byte-per-byte copying but
>that would be a waste of time.
>
>How about using the good ol' DOS copy function?
>Your problem is that you're not specifying
>binary mode for combining the two files. Here's
>what you should be doing:
>
>copy /b file1.in+file2.in+file3.in
>destination.out

I'm just trying to read a certain amount of bytes and write them to the disk... Are you telling me that I should call a system command line function instead of using C++ directly?

It's so hard and complicated????

I think I'm gonna try the fget and fput functions instead of read and write...



[p align=center]
MatuX
Co-Leader and Chief Programmer on
http://clanfusionn.hypermart.net/tmslogo1.gif
[font size=1]GFX by Smackrazor
[font size=2]http://www.modsquad.f2s.com[/p]
 
RE: Hmmm??

Try posting on comp.lang.c or comp.lang.c++. The only thing I can think of is a screwy file position that might throw it off. Try a call to _lseek before the second write.
 
RE: Hmmm??

Oh you could do it byte per byte if you want. Just open up a file for read/write and use fseek() to move to where you want to go, then copy bytes over with fread() and fwrite(). Remember to set binary mode when opening the file.

-Xotor-

>>You could do manual byte-per-byte copying but
>>that would be a waste of time.
>>
>>How about using the good ol' DOS copy function?
>>Your problem is that you're not specifying
>>binary mode for combining the two files. Here's
>>what you should be doing:
>>
>>copy /b file1.in+file2.in+file3.in
>>destination.out
>
>I'm just trying to read a
>certain amount of bytes and
>write them to the disk...
>Are you telling me that
>I should call a system
>command line function instead of
>using C++ directly?
>
>It's so hard and complicated????
>
>I think I'm gonna try the
>fget and fput functions instead
>of read and write...
>
>

>[p align=center]
>MatuX
>Co-Leader and Chief Programmer on
>http://clanfusionn.hypermart.net/tmslogo1.gif
>[font size=1]GFX by Smackrazor
>[font size=2]http://www.modsquad.f2s.com[/p]


[div align=center]

http://www.poseidonet.f2s.com/files/nostupid.gif
[/div]
 
I would try that, but...

Wouldn't it take toooooooo much time?

Remember that I have to copy files whose size can be up to 70mb.
In the other hand, doing a byte per byte copy I would have to forget the need to divide the buffer on long files...

I will check what Vasey told, too.

Thanks for the help guys, if I'm continuing having problems I will bother everyone even more.....;-)



[p align=center]
MatuX
Co-Leader and Chief Programmer on
http://clanfusionn.hypermart.net/tmslogo1.gif
[font size=1]GFX by Smackrazor
[font size=2]http://www.modsquad.f2s.com[/p]
 
Back
Top