TheSwamp
Code Red => .NET => Topic started by: Andrea on September 27, 2006, 03:04:32 PM
-
Hi all,
I'm new to C# and i'm trying to read a simple TXT file containing the "¦" symbol:
eg: test.txt
*line1 0¦4¦true¦open¦6¦72¦24¦
this is my code
int counter = 0;
string FILELINE;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(@"c:\test.txt");
while ((FILELINE = file.ReadLine()) != null)
{
//System.Console.WriteLine(line);
TAB1_listBox_file.Items.Add(FILELINE);
counter++;
}
file.Close();
result = *line1 04trueopen67224
any idea why ?
Also, if you know any good forum for C#....
please let me know....thanks. ^-^
-
Andrea;
As far as I know, you need to read about "Regular Expressions in C#", there are plenty of sites devoted to C# with a lot of tutorials, there is one here the .NET Forum in TheSwamp.
Place this in your class:
using System.Text.RegularExpressions;
HTH
-
Here is a little function, that might help or at least to have another approach, it is super basic and no error control was used:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
[CommandMethod("ReadFile2")]
public void ReadFile2()
{
string sFileName = "c:\\test.txt";
FileStream stream = null;
stream = File.OpenRead(sFileName);
byte[] text = new byte[stream.Length];
stream.Read(text, 0, (int)stream.Length);
stream.Close();
ASCIIEncoding encoding = new ASCIIEncoding();
string content = encoding.GetString(text);
// string pattern = "[*]";
string pat = @"\w+\b-\b\w+"; // this will find the word characters between "-"...
// for some reason I cannot get the split to work where this character is used: "¦", I need to read more about expressions
MatchCollection matches = Regex.Matches(content,pat);
foreach(Match match in matches)
{
CommandLinePrompts.Message(match.Value);
}
}
Hope that helps, I am also learning C#
-
Here is another function:
[CommandMethod("ReadFile")]
public void ReadFile()
{
FileStream file = new FileStream("c://test.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
sr.Close();
file.Close();
CommandLinePrompts.Message(s);
}
But, it simple reads the whole file.... does not take in account the finding of an specific text line....
-
And another, but requires more homework...
[CommandMethod("ReadFile3")]
public void ReadFile3()
{
string sFileName = "c:\\test.txt";
FileStream stream = null;
stream = File.OpenRead(sFileName);
byte[] text = new byte[stream.Length];
stream.Read(text, 0, (int)stream.Length);
stream.Close();
ASCIIEncoding encoding = new ASCIIEncoding();
string content = encoding.GetString(text);
string [] split = content.Split(new Char [] {'*', '¦'});
foreach (string s in split)
{
if (s.Trim() != "")
//{
//CommandLinePrompts.Message("\n");
CommandLinePrompts.Message(s);
//}
}
}
-
And another.... I noticed that the special character is ignored (as this was your first concern/question)
[CommandMethod("ReadFile4")]
public void ReadFile4()
{
FileStream file = new FileStream("c://test.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader reader = new StreamReader(file);
string sLine,str,sSpace = "\n";
do
{
sLine = reader.ReadLine();
str = string.Concat(sLine,sSpace);
CommandLinePrompts.Message(str);
} while (sLine != null);
reader.Close();
file.Close();
}
It returns in the AutoCAD command line:
Command: readfile4
This is only a test
Of a text line
*line1 04trueopen67224
*line1 04-trueopen67224
*line1 04true-open67224
Must be an appropriate way to read those characters.... ¦
-
Also, to the moderators, could be possible to move this topic into the .NET, since it is related to C# language and might get more responses there... I think
Thanks.
-
In case anybody else is wondering as it doesn't seem Andrea will put up his solution:
namespace ConsoleApplication5 {
class Program {
static void Main(string[ ] args) {
StreamReader sr = null;
try {
sr = new StreamReader(@"C:\Temp\Test\Test.txt", Encoding.UTF7, false);
while (sr.Peek() > -1) {
string input = sr.ReadLine();
Console.WriteLine(input);
}
} catch (System.Exception ex) {
Console.WriteLine(ex.Message);
} finally {
if (sr != null)
sr.Close();
}
}
}
}
Cheers,
Glenn.