Click here to Skip to main content
15,885,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m stuck at trying to read a file in C# but file created in C++ with .dat Extension.

C#
struct Head
{
    long lvalue;
    long fvalue;
}

--after this array of

struct fileinfo
{
    long sec;
    long data;
}



Actally .dat file is C++ written using fwrite() with above struct format.

I m Tried with below solution but not getting proper values
C#
BinaryReader b = new BinaryReader(File.Open(FileName, FileMode.Open));
              long length = (long)b.BaseStream.Length;


              while (pos < length)
              {
                    lngData = b.ReadInt64();
                   Console.WriteLine("data {0}:", lngData);
                   Debug.WriteLine("lngData {0}:  ", lngData);
                  pos += sizeof(long);
                  ////    }
                  // }
              }
              Console.Read();
          }


following data display i open file in binary editor

http://i.stack.imgur.com/33sq7.png[^]
Posted
Updated 24-Dec-13 3:41am
v7
Comments
[no name] 21-Dec-13 9:16am    
What is the content of the files?

Is it plain text or binary "image" of e.g. a c++ struct?
Rakesh Bairi 21-Dec-13 9:45am    
Yes It Contains data with Struct object
joginder-banger 21-Dec-13 9:24am    
can share you data in .dat Extension.??
Rakesh Bairi 21-Dec-13 9:50am    
It Contains Unreadable,
I m pasted some part of data
¾) ž"©$ ¾) 4 þa DŒ ŽÁ ¬A ! Àr
®€ $ >à Ä° Î? k ^Ÿ ¬C# îþ
Lì' ~^ 0Ý- ¾
ÔL3 ž €y: .} ®> ¾Ü ÄïD N< D¸J Þ› ä¾O nû ìU þZ ԁ[ Žº lÐ`  ”Mg ®y lm >Ù ˜\r Î8 àw ^˜ Ä{} î÷ „ ƒ ~W" p?‰ ·# °µŽ ž% P’• .v& ÿš ¾Õ' ”f  N5) Ð¥ Þ”* ü­ nô+ ”² þS- ÀÒ· Ž³. €`½ 0 äN ®r1 ïÇ >Ò2 è.Í Î14 ÀþÒ ^‘5 ØdØ îð6 <mþ ~p8="" ˜Òã="" °9="" dåé="" ž;="" x:ï="" .o<="" ¬kõ="" ¾Î="†û" n.?="" ="" ="" ލ@="" dÍ="" nía="" ð ="" þlc="" ˆ.="" Ž¬d="" h="" ="" f="" (¢="" ®kg=""  ƒ"="">ËH $(( Î*J ê- ^ŠK X»3 îéL è"9 ~IN ܲ> ©O dbD žQ 0+K .hR p»Q ¾ÇS <ÈW N'U ìË\ Þ†V ˜"b næW œóf þEY ´æl Ž¥Z xòq \ 0&x ®d] g} >Ä^ „⃠Î#` D㈠^ƒa XŽ îâb  W” ~Bd ä6š ¢e X¯Ÿ žg Ø¥ .ah [¬ ¾Ài ૱ N k X¢¶ Þl ü@¼ nßm ¨±Â þ>o è<Ç Žžp 8=Í þq 0”Ó ®]s ÜwÙ >½t (‚Þ Îv Dä ^|w àªé îÛx øÌï ~;z dëõ ›{ Äpú žú| ”ˆ  .Z~ è× ¾¹ L‚  N ˆz Þx‚ Ôâ n؃ <t þ7…="" ¤Ú"="" Ž—†="" p¸(="" ÷‡="" $%.="" ®v‰="" ”Á3="">¶Š Ä:9 ÎŒ @É> ^u €D îÔŽ °J ~4 ”P ”‘ ôwU žó’ Pj[ .S” |Ø` ¾²• ¦f N— À9l Þq˜ Ì r nÑ™ Hšw þ0› ï} Žœ ôÿ‚ ð \äˆ ®OŸ \Ž >¯  °B” ΢ X½™ ^n£ 8  îͤ ॠ~-¦ $Ϊ § ´° žì¨ h/¶ .Lª Ú¼ ¾«« Üÿ N ­ \™È Þj® LMÎ nʯ 
Marius Bancila 22-Dec-13 17:34pm    
How do you expect this gibberish to help us answering your question?

First you have to clarify what size and format long is in both environments.

C++ and C# long types are two independent entities:
- C++ can be anything from 32 bit and above.
- C# is defined as 64 bit entity.

If you have access to the C++ source code and if you can compile it yourself, check for
C++
#include <limits>
...
std::cout << "min long = " << std::numeric_limits<long>::min(); << std::endl;
std::cout << "max long = " << std::numeric_limits<long>::max(); << std::endl;


If max long results in 2147483647, you have a 32 bit long type in C++.
If max long results in 9223372036854775807, you have a 64 bit long type in C++.
If you have other values I would be surprised.

For 32 bit C++ long, try to read as C# Int32.
For 64 bit C++ long, try to read as C# Int64.

C++ structs may have padding bytes, but I guess with the given types, this is not an issue (no padding bytes).

Finally, the endian-ness might screw-up things too, i.e. if the C++ program runs on a big-endian system, the byte order is swapped compared to little-endian system. If the programs are compiled and run on the same system, this may usually be no issue, though.

If you design both applications (C++ and C#) yourself, you might consider to design a portable format that does not depend on padding bytes, endian-ness, and built-in type sizes (e.g. json[^], etc.).

Cheers
Andi
 
Share this answer
 
v2
The chances are that is doesn't contain text, but binary data - hence the .DAT extension. Unfortunately, there is no standard for .DAT files - I can think of a dozen or so applications that generate files with that extension, and all of them are completely incompatible...

There are two ways you can read the data:
1) (Esay way) Look at the C++ code and / or documentation to find out what the file format contains, and how it is stored.
2) (Hard way) If you don't have access to the C++ source or it's documentation, then you need to start with a hex editor and start looking at the data, and work out for yourself what the format is. Warning: This can be a long job, and there is no guarantee that you will get it right the first time. Or indeed the twenty-first time...

This may help: http://www.pspad.com/[^] - it's a text editor with a Hex mode (I use it quite a lot, and it's pretty good - free too).

When you have worked it out, this might help: ByteArrayBuilder - a StringBuilder for Bytes[^] - one of the reasons I wrote it was so that I could load binary files easily.
 
Share this answer
 
Comments
Rakesh Bairi 21-Dec-13 9:54am    
Thanks for your response.I will try with your suggestion
Rakesh Bairi 24-Dec-13 4:27am    
Actually i m trying with your solution , getting suggestions use binaryreader but i have data in .dat in struct format i m updating question please if u know any solution suggest me
OriginalGriff 24-Dec-13 4:59am    
Without knowing your data? We can't!
Rakesh Bairi 24-Dec-13 5:21am    
Data stored in struct object format
OriginalGriff 24-Dec-13 5:30am    
That means nothing! This is structured data:
C0762139C1B1346E80A6D106F50FA4CE212DBDCBC99D8A64B97D1F9A667A4C3D4FBCBC4A07CEB779B17E
And it is meaningful to my program. But to you? It makes no sense at all! :laugh:

Just because data is "structured" does not mean it is structured in the same way by all code.
The System.IO.BinaryReader Class[^] has methods to read the different numeric types.
 
Share this answer
 
Comments
Rakesh Bairi 24-Dec-13 5:06am    
Thanks for your reply actually i m tryied same thing also not getting expected result
Richard MacCutchan 24-Dec-13 6:43am    
Well I am afraid that information is not very useful. If you want assistance with this then you need to show the code you are using, the results you expect to see, and what you actually see. Please use the Improve question link above and add all the relevant information to your question.
Show us the exact code you use to write data and tell us the first few values that you are trying to write.

Usually, if you save binary data, the your struct should be annotated to tell the compiler how to align data and you have to ensure that everything match on both sides.

It is also a good idea to validate structure size whenever you write or read data directly so that any mismatch are reported. In C++, you can use static assertion (compile-time) and in C# you can use run-time assertion. You can then assert that your Head structure in 8 bytes (or 16 bytes) in both cases. If this is not the case, then you fix of on the two sides to match the other one.

If one of the format (created by C++ or C#) is already used, then usually, you would ajust the new one to match the existing one.

On the other hand, if you start a new project in both languages, then you can consider text format (json, xml) or you properly define the format byte per byte... In most cases, you will want to separate the actual file format from the memory representation...

Also, you should consider using type that have well defined size in your structure and swap bytes as required if the endianness does not match.

There are many possibilities but depending on performance and data size some might be more appropriate than others.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900