|
Hi,
I have a complicated (to me) problem reading/Writing data in binary. Here is what I am doing.
For example let say a text file has the following:
File: test.txt
-----------------
1234a5678b9
-----------------
Here is what I am doing:
istream inData;
ostream outData;
inData.open("test.txt", ios::in | ios::binary);
outData.open("modified.txt", ios::out | ios::binary);
inData.seekg(0, ios::end);
int size = inData.tellg();
char *temp;
temp = new char[size + 2];
inData.read(reinterpret_cast<char *="">(temp), size);
temp[size + 1] = 'c';
temp[size + 2] = 'd';
outData.write(reinterpret_cast<char *="">(temp), (size + 2);
-----------------------------------------------------
The code above should work okay. As you can see, all I am doing is reading data from a file, add two characters "c" and "d" and then writing it back to the file. Everything is fine.
Now, let say I read the modified file back.
Here is what I am doing:
istream modData;
ostream weird;
modData.open("modified.txt", ios::in | ios::binary);
weird.open("hungry", ios::out | ios::binary);
inData.seekg(0, ios::end);
int size = inData.tellg();
char *temp, charA, charB;
temp = new char[size];
inData.read(reinterpret_cast<char *="">(temp), size);
charA = temp[size - 2];
charB = temp[size - 1];
-----------------------------------------------------
Do you see what I am trying to doing?
1)read fileA in binary
2)save data from fileA to tempX
3)add characters to tempY
4)write temp to fileB
-----------------------
1)read fileB in binary
2)save data from fileB to tempZ
3)extract ONLY those characters I added to tempX from tempZ
-----------------------
The method I use to extract the added characters from the modified file after I read it back is subscript. I assume let say temp[5] is 'a'. When I read the data back, I look at temp[5].
My question is when you read a file in binary, add characters to it in specific spaces and write it to modified file, is it possible to read the data from the modified file and extract those characters you added via assumption subscript? How are data saved when you read and write in binary?
Thanks,
Kuphryn
|
|
|
|
|
I see some problems with what you are doing.
First, arrays are zero-based. To append two characters to the end of an array of size means you write to temp[size] and temp[size+1]. The write command with (size+2) is then correct. Accessing [size+2] means you must allocate and write (size+3) items. The last of the original data will be at index (size-1).
To extract only the characters you added then seek to position size on your input stream and then read 2 items.
|
|
|
|
|
Interesting. Thanks.
I got everything write. When I read teh modified data back, sometimes it comes out right and sometimes it does not. I do not know if it is writing the modified file differently everytime.
Kuphry
|
|
|
|
|
Is possible to rebuild IntelliSense ASC stores found in <msvs-root>\Common\MSDev98\Bin (Win32.ncb, Mfcatl.ncb and Crt.ncb) ? Simply deleting them only disables code editor's IntelliSense features
I'd needed to recreate them because they contains in some instances improper data, f.E. incorrect number of function parameters, wrong return type etc. - it could be very confusing.
Does anybody know how to solve this problem or knows something about internal format of .ncb files ? I know there is a good replacement called Visual Assist from WholeTomato software but it isn't free
Thx
Cernd
|
|
|
|
|
In my SDI MFC app there's a CMainFrame, and I don't want it to be resizable. How I can do that? Thanks for the help
|
|
|
|
|
Remove the WS_THICKFRAME style
Nish
|
|
|
|
|
Where can I find it??? in the CREATESTRUCT cs??? In the MSDN there isn't any help for the CREATESTRUCT
|
|
|
|
|
Hi,
I need to know that whether its possible to have server based on WinSock and Client based on CAsyncSocket?
I tried so but did not succeed.. Before i put my full efforts i need to nkow if it is possible or not..
If there is something special work needed to be done to make it work, then do specify it..
Thanking u in advance
RawCoder
|
|
|
|
|
|
If you have a network (LAN), go to your network connections in Control Panel and right click on the Local Area Connection (or whatever you have there). You can make it "Enable" or "Disable" (connect or disconnect). Now my question is how to do this programmatically in Windows 2000? I was looking in MSDN and didn't find nothing.... Change user info, get domains.... all what you want but not something like Connect or Login.
IF someone knows the answer, please tell me
Thanx
Philip Patrick
|
|
|
|
|
- Can you help?
- Regards,
Maer
|
|
|
|
|
if you are asking about #ifndef then the main difference is the #if form permits complex expressions such as :
#if( !define(THIS) && !define(THAT) && !(THE_OTHER_THING) )
I don't believe that #ifndef supports this type of expression. It may but I have never seen any.
|
|
|
|
|
- Thanks pal!
- I think the statement "#if( !define(THIS) && !define(THAT) && !(THE_OTHER_THING) )" should be
#if( !define(THIS) && !define(THAT) && !define(THE_OTHER_THING) )
- Am I right?
- Regards,
BigMouth
|
|
|
|
|
Yep, I missed that last define.
I was just testing you.
Actually, they should all say defined but your compiler would tell you that soon enough.
Good Luck.
|
|
|
|
|
- Thanks pal!
- I think you are smart and warm-hearted.
- Regards,
BigMouth
|
|
|
|
|
- Can you help?
- Regards,
Maer
|
|
|
|
|
_DEBUG is the standard definition that VC++ uses when performing a debug build. I don't know about DEBUG. I couldn't find it in the docs.
|
|
|
|
|
|
- Can you help?
- Regards,
Maer
|
|
|
|
|
It is used to prevent a header file from being included more than one time.
|
|
|
|
|
|
- I still have a question. I think we can use the statement "#if !defined ... #define" to do the same task. I think the use of "#pragma once" is a redundance. Am I right? (For example, in MainFrm.h of a MDI app)
- Can you help?
- Regards,
BigMouth
|
|
|
|
|
In most cases that I have seen it used it is redundant.
There are two ways to go about the issue of multiple include prevention. You can benignly allow it and protect against it in the header (like the AppWiz does) or you can require the includers to insure that they haven't seen it yet which is what I usually do.
I use this kind of thing :
#ifndef _THISHEADER_H
#define _THISHEADER_H
#else
#error repeated include of this file
#endif
Then in the source files I do this :
#ifndef _THISHEADER_H
#include "ThisHeader.h"
#endif
This insures that the header is included once and only once.
FWIW, I learned this from checking out the SDK headers. I added the error check so that I would find the cases where I missed the ifndefs.
|
|
|
|
|
- Thanks pal!
- I still have a question. I think the statement "#error repeated include of this file" should be #error _THISHEADER_H has already defined". I think in the header file we can only see whether the variable "_THISHEADER_H" has been defined. We can not see whether the header file has been include twice. Am I right?
- Can you help me out?
- Regards,
BigMouth
|
|
|
|
|
The reason I did it that way is that when the error message is displayed it tells you the line number and file name where it occurred.
Of course, you can make the error message say whatever you like.
|
|
|
|