|
As david said not every file has a header. There are as many file formats as grains of sand. I myself have invented at least a dozen file formats. There is no standard format for a header. You must know how to decode the file to read the header in most cases.
John
|
|
|
|
|
thank you both for your explaination.
|
|
|
|
|
Decode!!!
Can you please give me some example in decoding for one file?
for example, a pdf file.
I have to read in binary right. and then I really don't know how to continue from there.
|
|
|
|
|
For PDF search the internet for ghostscript. You do not want to write decoders for all file types. Writing one for PDF alone should take a person several months work. I was just thinking depending on what information you need you might be able to get the info you need from the pdf file without decoding the whole file.
John
|
|
|
|
|
Oh.. I think I misunderstood you saying decode.
here is my plan to know the file creator.
I need to collect all magic number or creator of each and every file and crate the list or table. for example,
file magic numbers
Then I will read may be first 100 byte of the file and see if i can find any magic number that exits in the table. By doing so, I can tell the file is created with the particular application.
So I don't have to worry about whether there is a file extension or weird file extension of the file. but I can still view/print the file with the correct application.
|
|
|
|
|
Ok. That will work for some file types but by no means all. A PDF file should always start with %PDF.
John
|
|
|
|
|
Ok. I thought you were still trying to extract the author and other properties from the file.
John
|
|
|
|
|
no I'm not going to change anything to the file but only to identify the creator of the file which doesn't have file extension.
so can i rely on the magic number of the file to identify the creator?
|
|
|
|
|
pnpfriend wrote:
so can i rely on the magic number of the file to identify the creator?
In the files that have a magic number for the most part yes. The problem is that sometimes a magic number is not at the beginning of the file. For the most part it is but sometimes it is not. A magic number is just a string of bytes and it is perfectly valid to be in any file so (although highly unlikely) you could find a magic number of one file type in the first few bytes of a different file type. You can only be 100% sure if you decode the file and this is way too difficult and too much work for any programmer. Teams of programmers spend years developing code to decode formats like PDF, JPEG, PNG ...
John
|
|
|
|
|
pnpfriend wrote:
no I'm not going to change anything to the file
Its not about changing the file but as I said in the last post sometimes you have to decode a file to verify it is the correct type but even this has its problems. New versions appear and the decoder of the old version will sometimes not be able to read the first version.
John
|
|
|
|
|
I hope I am not discouraging you at all. My point that this is not an easy task. You have to weigh many factors. Obviously you can not support all formats but you can support some of the most common ones with a great deal of accuracy. There is a web site that has the doccumentation for at least 100 different formats. I forgot the link but I have seen it posted on this site many times. You might want to post a question to the lounge asking for that. I think this type of question is permitted.
[EDIT]
Oh. I see you already found that in another thread... http://www.wotsit.org/[^]
[/EDIT]
John
|
|
|
|
|
Oh Please don't say that, you are not discouraging me at all..
But even to find the magic number in the first 100 byte is difficult? I have never done reading file in byte before
Would
BYTE b;
ifstream afile;
afile.open("c:\\k.pdf",ios::byte);
afile>>b;
be a correct way to do it?
Thank you very much and I'll do post a new thread for asking common file types..
|
|
|
|
|
That will read only one byte.
I never use streams. I either use the WIN32 api ReadFile() or MFC so I am exactly certian how to read a file with streams so I don't want to give you any wrong information. Post a question in the VC++ forum asking how to read the first 100 bytes of a binary file into and array using streams.
John
|
|
|
|
|
While working on a Win16 to Win32 convertion I was asked to modify the origanal Win16 (Ver 2.1) of the software. During testing of the Win16 (Ver2.2) we noticed a problem and upon look at the Win32 port we saw that it also the same problem.
Problem: We have a simple little loop that reads data from a file with out errors, but when the memory contains of the array of data is dumped to disk it contains no data. It is as if the file was full of 0x00. The test file only contains 2048 bytes of data. The Win16 code for reading is the same in both Ver 2.1 and Ver 2.2 [NO CHANGE], but it works in Ver 2.1. Also checking the Win32 beta port I have descover it to has the same problem.
Quesion: Does anyone have an idea of what is going on?
(Yes the file does contain valid data and can be view with an hex editor)
BOOL File_Read(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
)
{
if( !ReadFile(hFile,lpBuffer,nNumberOfBytesToRead,lpNumberOfBytesRead,NULL) )
{
TCHAR szError[512];
DWORD dwLen = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
szError, sizeof(szError)/sizeof(TCHAR), NULL);
if( dwLen )
Vsl_MessageBox(ghwnd,szError,gszAppName,MB_OK|MB_ICONSTOP);
return FALSE;
}
return TRUE;
}
lpBinFont = (LPBIN_FONT)GlobalAllocPtr(GPTR,(DWORD)sizeof(BIN_FONT)+dwFLen);
if( !lpBinFont )
return(NULL);
.....
DWORD nNumberOfBytesToRead = 0x0800L;
SetFilePointer(hFontFile,0,NULL,FILE_BEGIN);
dwRead = 0;
for( dwOffset=0;dwOffset<dwFLen;dwOffset+=dwRead )
{
if( nNumberOfBytesToRead > (dwFLen-dwOffset) )
nNumberOfBytesToRead = dwFLen - dwOffset;
if( !File_Read(hFontFile,(LPVOID)((lpBinFont->DataBits)+dwOffset),
nNumberOfBytesToRead,&dwRead,NULL) )
{
break;
}
if( dwRead < nNumberOfBytesToRead )
{
dwOffset += dwRead;
break;
}
}
lpBinFont = (LPBIN_FONT)GlobalAllocPtr(GPTR,(DWORD)sizeof(BIN_FONT)+dwFLen);
if( !lpBinFont )
return(NULL);
.....
_llseek(hFontFile,0,0);
for( dwOffset=0;dwOffset<dwFLen;dwOffset+=uRet )
{
uRet = _lread(hFontFile,(lpBinFont->DataBits)+dwOffset,0x0800U);
if( uRet == HFILE_ERROR )
break;
if( uRet < 0x0800U )
{
dwOffset += uRet;
break;
}
}
Trust in the code Luke. Yea right!
|
|
|
|
|
More Info: Newer binaray file are not being read correctly [ALL VERSIONS] but older files are loaded just fine. The same program was used to create the old binaray files and the new files. One more thing, a second routine reads the data from the same file before the file is closed and has no problem getting the data.
Trust in the code Luke. Yea right!
|
|
|
|
|
Solution (Win16):
uRet = _lread(hFontFile,(BYTE HUGE*)(lpBinFont->DataBits)+dwOffset,0x0800U);
BLASTED @#^$& 16 BIT CODE ANY WAY.
Trust in the code Luke. Yea right!
|
|
|
|
|
I am reading data from memory on a pci card and all the values are of double type.
I really just want to read one huge chunck of size 32bytes and parse each double out of it.
I don't know how I can do that with the given *.dll so here I am.
I thought I would read in a 32bit at a time from: memory address i ----- memory address i + 1........
then I would cast them to a 64 bit long. The code below might explain it better.
I know I am doing something, just don't know what it is. The compiler says I have too many types in the decliration.
Thanks, steven
[code]
double buf[2];
struct{
unsigned first;
unsigned next;
} in_data;
for(i=0; i<2; i++){
in_data.first = rfmRead(rh, , 0x40, buf, sizeof(buf));//address 1
in_data.next = rfmRead(rh, , 0x41, buf, sizeof(buf));//address 2
}
double dd = *(unsigned long long *) &in_data;
[/code]
|
|
|
|
|
By the time you parse each double individually, I would think you would have been better off reading as double in the first place. (you can do some benchmarking to see) If the number of memory locations varies, you could always use a std::vector to hold the values and provide any necessary interfaces to it.
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Hello everyone.
I have a dialog to show the scores of a game, and I put a list box in that dialog. I want to show the names and the scores in a table. I just can't find a reference to tell me anything about how to make columns in a list box.
I tried something else to temporarily solve the problem, but something is going wrong. I wrote the following piece of code:
CString Score, Name, Final;
CRect ScoreRect (0,0,0,0), MyListRect;
CListBox *MyList = static_cast < CListBox* > ( GetDlgItem ( IDC_MYLIST ) );
CDC* dc = MyList->GetDC();
MyScoreFile.ReadString ( Name );
MyScoreFile.ReadString ( Score );
MyList->GetWindowRect ( MyListRect );
while ( ScoreRect.Width() < MyListRect.Width() )
{
Name += " ";
Final.Format ( "%s%s" , Name , Score );
dc->DrawText ( Final , ScoreRect , DT_NOCLIP | DT_CALCRECT );
}
MyList->AddString ( Final );
The problem is that the text does not appear taking the whole width of the list box, but rather a small region, about two thirds of the width of the list box. I also tried to use MyList->GetClientRect ( MyListRect ) with the same results.
Thank you very much.
<marquee>Hosam Aly Mahmoud
|
|
|
|
|
This seems like a very convoluted way to be using a listbox. If you are restricted to using a listbox, a simpler approach would be to turn on the listbox's LBS_USETABSTOPS style. Then populate it with something like:
while (! bDone)
{
MyScoreFile.ReadString ( Name );
MyScoreFile.ReadString ( Score );
Final.Format ( "%s\t%s" , Name , Score );
MyList->AddString ( Final );
}
No doubt you'll need to adjust the "width" of the columns (as 32 DLUs are probably not going to be enough for a name-type column) by calling SetTabStops().
That said, I would use a list control (in report mode) instead.
|
|
|
|
|
Thank you very much for your reply.
DavidCrow wrote:
I would use a list control (in report mode) instead.
I really thank you for this advice. Until the time I posted my question, I did not even know that there is something called a list control that is different from a list box. Meanwhile, I now know it exists. I tried to use it on my own (with the help of MSDN), but I could not get the results I wanted (yet). I would be very thankful if you could tell me of an article that explains the subject in details.
Thanky you again.
<marquee>Hosam Aly Mahmoud
|
|
|
|
|
Hosam Aly Mahmoud wrote:
I tried to use it on my own (with the help of MSDN), but I could not get the results I wanted (yet). I would be very thankful if you could tell me of an article that explains the subject in details.
Examples are bountiful, both on MSDN and the Internet (use Google). What exactly is not working for you?
|
|
|
|
|
Hello
Im using serial communicating (COM3 and COM4) to talk with another server.
I use the exact same settings and the same class (overlapped io with readfile/writefile) to communicate with the server (diffrent protocols).
Everything runs smoothly on COM3, but the overlapped read doesn’t complete for COM4 (as nothing is received).
If I connect to COM4 using hyper terminal everything works (both send and receive).
What can be wrong?
Thanks,
Jonas
|
|
|
|
|
Anyone know of a good logging utility for C++? Something that is thread safe as well.
I have looked at log4cpp but I am not sure how reliable it is.
anyhow, thanks
|
|
|
|
|
A "logging utility" is fairly vague. Can you be more specific?
|
|
|
|