I am having a problem writing to files.
The problem is that I do not get the same result when running on windows xp and windows server 2003 as I do when I run it on a windows server 2008 machine.
The original code was from a larger program but after having debugged this for a day I decided to make a test-program.
My objective: To read a file, after having read 200 lines write lines to new file.
My result: On windows xp and 2003 machines I get 200 lines in new file. On windows 2008 machines I get 218 lines in new file.
Below is a simple test which for me behaves differently on win xp and 2008.
What am I doing wrong?
Code:
private void ProcessFile(string inputFileName, string outputFileName)
{
Encoding encoding = Encoding.GetEncoding(65001);
List<string> transactionlist = new List<string>();
StreamReader sr = new StreamReader(inputFileName, encoding);
List<string> splittedFiles = new List<string>();
int lineCount = 0;
int maxLinesFile = 200;
while (sr.Peek() != -1)
{
lineCount++;
transactionlist.Add(sr.ReadLine());
if (lineCount == maxLinesFile)
break;
}
sr.Close();
sr.Dispose();
File.WriteAllLines(outputFileName, transactionlist.ToArray(), encoding);
return;
}
Loading
Niklas BuhuPosted Jun 1, 2010, 3:15 AM
I am sad to say I had to give up and install windows 2003 instead on the machine.
Then the program works fine. I must say though that I am perplexed what is the reason I can't get it to run on windows server 2008.
I have a final thought:
I tried to take the code from NLog (a logger library available on the net), because when loggin with this I can write 200 separete log-lines fine without problems. I created my filestream like they did, did everything ekstra carefully and still the same result.
So I was thinking, is it possible it is not the target machines that is at fault, but that it is something going wrong when I build it on the developer machine? (But I've tried changing target from 3.5 to 2.0 without a difference to the result).
Anyways, thank you all for your help. There is still one Windows server 2008 machine I can test on if anyone has a good suggestion.
Niklas BuhuPosted May 28, 2010, 4:23 PM
PJ MartinsPosted May 28, 2010, 3:12 PM
Niklas BuhuPosted May 28, 2010, 10:39 AM
I am now convinced maybe something is wrong with both the windows server 2008 machines I have tested this on, even though the exact same thing happens on those 2.
I'll explain.
If I write out static text, flush after each write I get the same error. The error is the same even when I change buffer size.
Same error despite using different methods, write through File, Filestream,Streamwriter or even filestream from kernel32.createfile.
I have tried writealllines(), writeline and write stating different encodings and buffersizes.
I converted the strings to char[] to byte[], using write(byte[], 0 byte[].length) and still same same.
So in short, I can recreate this using any text, using any methods.
That it happens on 2 different machines just throws me completely off track :S
But I am pretty sure my code is correct (thank you also to PJ martins for checking a earlier version of my test), and this is just too obvious to be bug, so only thing I can figure out is that my .net framework must be messed up. I've tried reinstalling through add/remove features but no difference. Have also tried sfc /scannow.
Anyone with any suggestions, or anyone who is able to test this on a windows server 2008 to confirm that this error must be specific to just my installations?
Sam HobbsPosted May 28, 2010, 7:46 AM
Instead of using a text editor such as notepad, I would compare the files using a binary viewer. In fact, Visual Studio is a better text editor than notepad. Using VS, you can open a file using either a binary or text editor. Open the two files in VS using the binary editor and use a split window to compare them.
PJ MartinsPosted May 27, 2010, 7:24 PM
PJ MartinsPosted May 27, 2010, 9:05 AM
Niklas BuhuPosted May 27, 2010, 2:26 AM
I need the encoding as it reads files from various countries, it decides the encoding depending on the origin of the file. In this debug though I have just decided on the encoding (65001);
Here is the code I tried with;
Encoding encoding = Encoding.GetEncoding(65001);
StreamReader sr = new StreamReader(inputFileName, encoding);
StreamWriter sw = new StreamWriter(outputFileName, false, encoding, 1024);
int lineCount = 0;
int maxLinesFile = 200;
while (sr.Peek() != -1 && lineCount < maxLinesFile)
{
lineCount++;
sw.WriteLine(sr.ReadLine());
}
sr.Close();
sr.Dispose();
sw.Close();
sw.Dispose();
I tried changing buffer size, but it made no difference.
Thank you for your quick response, I hope you can help me further also.
PJ MartinsPosted May 26, 2010, 8:57 PM
StreamReader sr = new StreamReader(inputFileName, encoding);
StreamWriter sw = new StreamWriter(outputFileName, encoding);
int lineCount = 0;
int maxLinesFile = 200;
while (sr.Peek() != -1 && lineCount < maxLinesFile)
{
lineCount++;
sw.WriteLine(sr.ReadLine());
}
sr.Close();
sr.Dispose();
sw.Close();
sw.Dispose();
Niklas BuhuPosted May 26, 2010, 6:04 PM
The ekstra 18 lines are really 17½ lines.
It repeats the content from line 91 and a half.
The files are similar in lengths of each line. I was thinking this might be a buffer problem but I am experiencing the same scenario every time and on 2 different machines.
Thank you for answering, I was afraid not to get any responses at all and whatever I am doing wrong I can't figure it out.
PJ MartinsPosted May 26, 2010, 5:41 PM