|
I am having some issues using a Filestream to read a text file. The file contains repeated segments of text in the format shown below. when I attempt to read it, it will not read the entire file, it will stop at the line shown (this line is at a variable position, there can be any number of lines before it). However... if I open the text file from windows explorer and save it (not altering the text), it has no problem reading the entire file the next time the program runs.
Anyone have any ideas, or have run into this problem before?
UPDATE: After doing some more testing, I've found that FileStream IS reading to the buffer, as I can write the contents of the buffer to a new text document, and the entire file is copied. The error appears to be occuring somewhere else. I have also tried the StreamReader class and it has the same issue.
FileInfo m_fileInfo = new FileInfo("C:\\Programs\\PartyGaming\\PartyPoker\\HandHistory\\xxxxxxxxxx\\xxxxxxxxxx\\Speed #xxxxxxx_xxxxxxx.txt");
long length = m_FileInfo.Length;
byte[] buffer = new byte[length];
using (FileStream stream = new FileStream(m_FileInfo.FullName, FileMode.Open, FileAccess.Read))
{
for (long i = 0; i != length; i++)
buffer[i] = (byte)stream.ReadByte();
}
After reading to byte array I am converting to a string for further process by a StreamReader object
string data = new System.Text.UTF8Encoding(true).GetString(buffer);
MessageBox.Show(data);
The file.....
Game #xxxxxxxxxx starts.
#Game No : xxxxxxxxxx
***** Hand History for Game xxxxxxxxxx *****
$10 USD NL Texas Hold em - Wednesday, July xx, xxxxx EDT 2009
Table Speed #xxxxxxx (Real Money)
Seat 4 is the button
Total number of players : 9
Seat 7: xxxxx ( $9.11 USD )
Seat 1: xxxxx ( $11.32 USD )
Seat 6: xxxxx ( $10 USD )
Seat 8: xxxxx ( $2 USD )
Seat 9: xxxxx ( $12.33 USD )
Seat 4: xxxxx ( $14.93 USD )
Seat 2: xxxxx ( $2.63 USD )
Seat 5: xxxxx ( $14.96 USD )
Seat 3: xxxxx ( $18.61 USD )
xxxxx posts small blind [$0.05 USD].
xxxxx posts big blind [$0.10 USD].
** Dealing down cards **
Dealt to xxxxx [ Qs 2h ]
xxxxx calls [$0.10 USD]
xxxxx calls [$0.10 USD]
xxxxx raises [$0.20 USD]
xxxxx folds
xxxxx folds
xxxxx folds
xxxxx calls [$0.15 USD]
xxxxx calls [$0.10 USD]
xxxxx calls [$0.10 USD]
xxxxx calls [$0.10 USD]
** Dealing Flop ** [ 4s, 6d, 9s ]
xxxxx checks
xxxxx checks
xxxxx bets [$0.71 USD]
xxxxx folds
xxxxx raises [$1.42 USD]
xxxxx folds
xxxxx folds
xxxxx calls [$0.71 USD]
** Dealing Turn ** [ 8h ]
xxxxx bets [$0.10 USD]
xxxxx calls [$0.10 USD]
** Dealing River ** [ 7d ]
xxxxx bets [$2.88 USD]
xxxxx folds
xxxxx does not show cards.
xxxxx wins $6.72 USD
Game #xxxxxxxxxx starts.
Any help would be appreciated
modified on Sunday, August 16, 2009 7:48 AM
|
|
|
|
|
Hi,
I'm not sure why it would fail.
You should accept what gets returned by FileStream.ReadByte and check for <0 to detect end-of-file; maybe that holds a surprise.
Anyway, you can replace all the input code by a simple File.ReadAllBytes().
Furthermore, as the file seems to contain text only, why are you handling it as a binary file?
You could call ReadLine() on it; or even use File.ReadAllLines().
I suggest you spend some time reading up on the File class!
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I have my reasons for this, I am new to reading files, but I intend to be able to either read the whole file, or move to a specified start point and read from that point. I did look through the other classes available and it seemed FileStream was the only class capable of this (maybe I missed something). I had just simplified my code in an attempt to try and find what was going wrong.
modified on Saturday, August 15, 2009 10:17 PM
|
|
|
|
|
Here's a guess as to what is happening.
The code that is creating the file runs and writes part of the file while keeping the file open. (I suspect it does this for each game.)
The code that you show is then triggered so it reads the file but only up to the point that has been written (i.e to end of first game.)
When you close the program the entire file is written so the next time you run it it works.
BTW regarding your use of ReadByte, you can use Seek to move about in the file (if it supports it) but this is probably only useful if the file contains fixed length 'records' with one record per line. I cannot see how this would be useful in the example file.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
It is my intention to run this while another program writes to the text file. But I am not to that point yet. I am not running the other program while testing my own code here. These are previously saved files I'm attempting to read.
|
|
|
|
|
I think this is just a MessageBox problem.
I tried it with the two lines you added and got the impression from the MessageBox that only part of the file had been read.
However, not so - it has all been read.
To check this I added a textbox, set its multi-line property to True and added vertical scroll bars.
I then set textbox1.Text = data; - result: all present and correct.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
The messagebox shows a partial file, but when I open the file from win explorer and save the file (I'm not sure what this actually affects), then messagebox will show the complete file the next time the program is run. I forgot to point out in original post that the line where the file always ends (if not opened and saved) is at a variable position, there can be any number of lines before this, but this is where it will always end (Player wins $xxx).
|
|
|
|
|
I can't reproduce this problem so don't know how to solve it.
There is the possibility that it is just a MessageBox issue.
I created a form as I said before with button and multiline textbox then added this code as click handler.
private void button1_Click(object sender, EventArgs e)
{
FileInfo m_FileInfo = new FileInfo("C:\\Playpen\\poker.txt");
long fileLen = m_FileInfo.Length;
byte[] buffer = new byte[fileLen];
using (FileStream stream = new FileStream(m_FileInfo.FullName, FileMode.Open, FileAccess.Read))
{
for (long i = 0; i != fileLen; i++)
buffer[i] = (byte)stream.ReadByte();
}
string data = new System.Text.UTF8Encoding(true).GetString(buffer);
long strLen = data.Length;
MessageBox.Show("Len of buffer: " + fileLen.ToString() + "\r\nLen of data : " + strLen.ToString());
textBox1.Text = data;
MessageBox.Show(data);
}
Set a breakpoint on 'string data = ...' and then step through it.
The data string does contain whole of file. And this is confirmed by the textbox contents.
I created poker.txt by copying your text and doing multiple pastes using Notepad. poker_utf8.txt is the same file saved as UTF-8.
(You get a slight discrepancy in lengths when using poker_utf8 because the UTF-8 has a three byte BOM as a prefix.)
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks for the help David, I appreciate it.
I have tried this stuff already and I made an edit to original post, saying it is reading to the buffer and the issue is elsewhere (size of byte[] = size of string = size shown in file properties) (I should have made a reply pointing to the updated info also).
I don't know anything about the encoding though, I tried utf8 as it was in the msdn sample, and it worked for the first section of the file, so I assumed this wasnt the issue.
Also worth noting that after this code the string is put through a StringReader where I run regular expressions on it and convert the data from the hand info into a 'PokerHand' object, if I run the program without doing the 'save thing' each file returns 1 'PokerHand' object, if I do the 'save thing' it returns as many that are in the file.
|
|
|
|
|
I have an image with letters in it, the letters are in two colors black and blue, I want to read the blue colored letters from the image.
Can anyone suggest me a method to do this in C#. Iam studying GDI+,but still didn't get any logic to develop this program..
Any guidance is highly appreciated.
Thanks,
Sumit Ghosh
Globussoft
|
|
|
|
|
|
Every1 talk about web browser in Java or C++ etc. etc.
But for the first tm I took up the challenge in .NET environment as my college final yr project...(Although forecasting as Simple & Best for my level..not extra junk... but yet essentially should be very viable)
Not By using C# GUI Drag & Drop Web Browser Control component already available in Visual Studio...But my very own web browser...
Can sm1 pls Clearly state me with the major modules that would clearly specify the diff. parts of my project....
I m still pursuing .NET course(completed C# currently started ASP) & m contemplating a lot to travel on a well defined line of course...cos time is less....
|
|
|
|
|
|
Hey guys!
Can somebody help me to make a Ip scanner for Server and client. And when the Client can connect one IP i want to show me the ip in a TextBox. He should scan all ip´s from x to y and every ip he can connect should he show me in a textbox!
|
|
|
|
|
|
i have seen that scanner. but how can i check all ip´s if i can connect them?
I want that i can see all IP´s which i can connect in the textbox but i don´t know how!
|
|
|
|
|
why don't you try connecting to each ip you get.
Or are you asking how to get list of ip from textbox
|
|
|
|
|
"why don't you try connecting to each ip you get."
Yes but then he will always connect the same IP!
And i want to show me all IP´s which i can connect in a textbox.
Then i have a other textbox for the connecting there i enter the Ip(from the textbox where i can see all open ip´s) that i want.
|
|
|
|
|
I dont quite follow you, because it is hard to understand.
PC17 wrote: Yes but then he will always connect the same IP!
Are you feeding him same ip?
PC17 wrote: And i want to show me all IP
So you use textbox for showing IP? You coud have selected multiline property to true for textbox or did you use ListView or ListBox control for showing?
Doues your control shows diffrent ip?
Or you use only 2 textboxes that have range of scan?
|
|
|
|
|
hello ,
try to implement this algorithm
foreath(ipaddress ip in GetAllIps()){
if( CreateThread(Ping(ip))){
AddToList(ip);
}
}
|
|
|
|
|
|
www.ebguru.com
This Site provides free download links for e books download, if you don't find a book,
you can request for that book and book download link will be emailed to you.
|
|
|
|
|
All books usually have copyrights ,and sharing them with making illegal copies is against the law.
Life is a stage and we are all actors!
|
|
|
|
|
Please don't post links to illegal websites.
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Hi,
kindly let me know that, how may I make following string into "double" with ROUND method?
string strRate = "195.9557";
thankx in advance
(Riaz)
|
|
|
|