View previous topic :: View next topic |
Author |
Message |
blackmorpheus Expert Cheater
Reputation: 0
Joined: 05 Apr 2008 Posts: 159
|
Posted: Mon Oct 26, 2009 12:14 pm Post subject: C# map parse help. |
|
|
Hello,
I need to parse a .map file which is being used by a game to draw the map.
The BinaryReader class doesn't provide what I need, i need a function to read every byte and store them into a string until it encounters 0x0D 0x0A
Which marks an end of line.
The ReadString function of BinaryReader doesn't stop reading the bytes there, it just reads them as \r\n.. But I want it to stop reading there..
Is there a way to do it?
Or is there another way how i can easily parse a data file in c# or c++?
I hope I'm clear enough,
blackmorpeus.
|
|
Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Mon Oct 26, 2009 1:41 pm Post subject: |
|
|
In c++ you can use the function getline like this:
Code: |
char Inputline[128]; //Depends on how big the lines in the file are
ifstream file("myfile.map");
if( file.is_open() ){
while( file.eof() == false ){
file.getline(Inputline, sizeof(Inputline));
//Process the line
}
file.close();
}
|
|
|
Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Tue Oct 27, 2009 5:48 am Post subject: |
|
|
Regular expressions are good for this: Code: |
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
...........
using (TextReader fileReader = new StreamReader(filename ))
{
string line = "";
MatchCollection file = Regex.Matches(fileReader.ReadToEnd(),@"(?<=^).+(?=$)",RegexOptions.Multiline);
foreach(Match lineMatch in file)
{
line = lineMatch.Value;
}
}
|
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
Back to top |
|
 |
blackmorpheus Expert Cheater
Reputation: 0
Joined: 05 Apr 2008 Posts: 159
|
Posted: Tue Oct 27, 2009 10:38 am Post subject: |
|
|
Thanks all, I solved it:
Code: |
public class MapReader : BinaryReader
{
private BinaryReader reader;
Stream mapStream;
public MapReader(Stream file) : base(file)
{
this.mapStream = file;
}
public override String ReadString()
{
int currentPosition = (int)mapStream.Position;
int enterPlace = 0;
for (int i = 0; i < 100; i++)
{
if (this.ReadChar() == 0x0A)
{
enterPlace = i;
break;
}
}
string line = "";
mapStream.Position = currentPosition;
line = new string(this.ReadChars(enterPlace - 1));
this.ReadByte();
this.ReadByte();
return line;
}
}
| [/code]
|
|
Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Wed Oct 28, 2009 10:34 am Post subject: |
|
|
See the binary reader works just fine..
_________________
Intel over amd yes. |
|
Back to top |
|
 |
blackmorpheus Expert Cheater
Reputation: 0
Joined: 05 Apr 2008 Posts: 159
|
Posted: Thu Oct 29, 2009 1:50 pm Post subject: |
|
|
Yeah it worked fine, but it didn't do what i wanted it to do.
What i wanted was a ReadString() function that stopped when there was a 0x0D 0x0A byte found
|
|
Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Thu Oct 29, 2009 3:24 pm Post subject: |
|
|
blackmorpheus wrote: | Yeah it worked fine, but it didn't do what i wanted it to do.
What i wanted was a ReadString() function that stopped when there was a 0x0D 0x0A byte found  |
Then wouldn't you need a different class than Binaryreader? Since Binaryreader is meant to read binary files which usually don't use 0x0D 0x0A as seperation.
|
|
Back to top |
|
 |
|