FileStream runtime issues (Updated) [modified]
-
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
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 -
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
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 foldsHi, 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.
-
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
-
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
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 foldsHere'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
-
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
-
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
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 foldsI 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
-
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).
-
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_utf8.txt");
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
}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
-
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_utf8.txt");
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))
{
// Read data from file to the buffer
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); // Shows incomplete file
}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.