Click to See Complete Forum and Search --> : Small problem Reading file
MemoreX
September 14th, 2009, 10:03 AM
Hello,
I recently started C# coding and I think i've done a pretty good job uptill now but I got stuck when trying to read a filetype called QSD.
It has this in it:
:FOREACH( condition_count )
DWORD length
DWORD command
BYTE[length - 8] data
:ENDFOR
:FOREACH( action_count )
DWORD length
DWORD command
BYTE[length - 8] data
:ENDFOR
I know how to read the Length and command DWORDs but not how to read that BYTE. I don't understand what I need to do with the Length-8 thing.
Can someone please explain?
someuser77
September 14th, 2009, 01:24 PM
Well I am not familiar with the QSD file structure but if you are using the BinaryReader class you can use the ReadBytes (http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes.aspx) method which takes the number of bytes to read, which will be the value you got from length minus 8 because you already got 4 bytes with the length and 4 bytes with the command.
MemoreX
September 14th, 2009, 01:53 PM
here this is what I have uptil now:
<Structure of QSD file>
DWORD file_version
DWORD block_count
SSTR qsd_name
:FOREACH( block_count )
DWORD record_count
SSTR block_name
:FOREACH( record_count )
BYTE check_next
DWORD condition_count
DWORD action_count
SSTR record_name
:FOREACH( condition_count )
DWORD length
DWORD command
BYTE[length - 8] data
:ENDFOR
:FOREACH( action_count )
DWORD length
DWORD command
BYTE[length - 8] data
:ENDFOR
:ENDFOR
:ENDFOR
And this is what I have sofar:
public class QSDConditions
{
public int length;
public int command;
public byte[] data;
}
public void OpenQSD(string filePath)
{
FilePath = filePath;
BinaryReader fh = new BinaryReader(File.OpenRead(FilePath));
//Basic info. Maybe usefull to display later?
fileversion = fh.ReadInt32();
// This is just the basis info and allows us to start.
BlockCount = fh.ReadInt32();
QSDname = Encoding.GetEncoding("EUC-KR").GetString(fh.ReadBytes(fh.ReadInt16())).Trim("\0");
blocks = new List<QSDBlocks>(BlockCount);
//oke first ForEach
for (int i = 0; i < BlockCount; i++)
{
blocks.Add(new QSDBlocks()
{
record_count = fh.ReadInt32(),
blockname = Encoding.GetEncoding("EUC-KR").GetString(fh.ReadBytes(fh.ReadInt16())).Trim("\0")
});
int recordcount = blocks[i].record_count;
blocks[i].records = new List<QSDRecords>(blocks[i].record_count);
//Records now for each block. Unsure about the LIST<> though.
for (int j = 0; j < recordcount; j++)
{
blocks[i].records.Add(new QSDRecords()
{
check_next = fh.ReadByte(),
condition_count = fh.ReadInt32(),
action_count = fh.ReadInt32(),
record_name = Encoding.GetEncoding("EUC-KR").GetString(fh.ReadBytes(fh.ReadInt16())).Trim("\0")
});
int conditioncount = blocks[i].records[j].condition_count;
int actioncount = blocks[i].records[j].action_count;
blocks[i].records[j].Conditions = new List<QSDConditions>(blocks[i].records[j].condition_count);
//Quest Conditions reader
for (int k = 0; k < conditioncount; k++)
{
blocks[i].records[j].Conditions.Add(new QSDConditions()
{
length = fh.ReadInt32(),
command = fh.ReadInt32(),
data =
});
}
}
}
I havent continued since I ran stuck on the data. I didnt paste evrything but I think I copied all that is important.
MemoreX
September 14th, 2009, 04:34 PM
Alright, I found the problem was quite obvious now I looked at it...
[Length - 8]
Length is 32 Bytes.
Length - 8 = 24 bytes.
So i just had to do
ReadBytes(24)
Thanks Someuser didnt think of something that easy actually.
Now I just need to find a way to read OpCodes/Transfer int into OpCodes
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.