Ok first I want to apologize for my disorganized thinking. I have tried so
many things in trying to get the code to work and give the correct output
that they are getting a bit confused in my brain.
Ok below is the complete code without any cuts. The file I am trying to read
in looks like:
5.62 1.49 6.53 3.91 3.26 3.04 4.47 2.58 2.01 2.00 2.68
4.17
2.85 5.78 6.02 4.65 3.25 4.45 4.73 1.60 2.76 1.75 6.82
0.29
4.41 5.52 12.51 6.89 1.84 2.31 3.70 3.07 0.08 1.10 2.74
6.83
8.22 1.58 4.21 5.29 1.93 3.34 4.38 3.54 4.68 3.56 3.94
2.30
5.86 6.02 8.18 3.69 2.56 2.17 4.68 2.24 0.98 2.03 2.02
5.95
2.95 6.51 3.88 5.42 2.61 11.27 3.32 1.80 4.54 5.07 6.01
2.65
4.58 2.32 4.22 5.78 4.31 3.02 1.77 7.21 4.29 1.00 1.16
7.69
5.22 4.02 8.81 3.06 2.60 3.51 1.24 3.07 5.00 2.59 4.70
6.60
in the text file. When I run this program I get the following:
5.62;
;
;
1.49;
;
;
6.53;
;
;
etc.
The other person running this code gets
5.62;
1.49;
etc.
using System;
using System.IO;
namespace csConsole
{
class NULL
{
static void Main(string[] args)
{
//open the file
StreamReader reader = new StreamReader("C:\\ChristineWork\\test1.txt");
string text = string.Empty; //to store the text
while( reader.Peek() != -1 )//while we're not at the end of the file
{
text += reader.ReadLine(); //append the line of text
}
reader.Close(); //close file
string[] parts = text.Split(' '); //split text by space
//the last element in parts is a NULL
//so I wrote Length-1
//the ';' I added to look if there are really no spaces left.
for(int i = 0; i < (parts.Length - 1); i++)
{
Console.WriteLine( parts
+ ";" );
}
Console.Read();
}
}
}
Jon Skeet said:
Christine said:
Ok I guess this one uses split, but it is the code I know works elsewhere.
using System;
using System.IO;
namespace csConsole
class NULL
static void Main(string[] args)
StreamReader reader = new StreamReader("C:\\ChristineWork\\test1.txt");
string text = string.Empty;
while( reader.Peek() != -1 )
text += reader.ReadLine();
reader.Close();
string[] parts = text.Split(' ');
for(int i = 0; i < (parts.Length - 1); i++)
Console.WriteLine( parts + ";" );
Console.Read();
Well, that's not a valid C# program, but some problems with it:
1) Using Peek is generally a bad idea. Just call ReadLine until it
returns null.
2) *Don't* use string concatenation like that - use a StringBuilder.
Now, you've said this code works elsewhere - is that the actual code
which fails on your box? If so, in what way does it fail?