Author Topic: c# reading file  (Read 4091 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
c# reading file
« 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

Code: [Select]
            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. ^-^
« Last Edit: September 27, 2006, 03:06:05 PM by Andrea »
Keep smile...

LE

  • Guest
Re: c# reading file
« Reply #1 on: September 27, 2006, 05:05:58 PM »
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:

Quote
using System.Text.RegularExpressions;

HTH

LE

  • Guest
Re: c# reading file
« Reply #2 on: September 27, 2006, 10:23:41 PM »
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:

Code: [Select]
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#

LE

  • Guest
Re: c# reading file
« Reply #3 on: September 27, 2006, 10:28:54 PM »
Here is another function:

Code: [Select]
[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....

LE

  • Guest
Re: c# reading file
« Reply #4 on: September 27, 2006, 10:33:54 PM »
And another, but requires more homework...

Code: [Select]
[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);
//}
}
}
« Last Edit: September 27, 2006, 10:35:39 PM by LE »

LE

  • Guest
Re: c# reading file
« Reply #5 on: September 28, 2006, 11:09:42 AM »
And another.... I noticed that the special character is ignored (as this was your first concern/question)

Code: [Select]
[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:

Quote
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.... ¦


LE

  • Guest
Re: c# reading file
« Reply #6 on: September 28, 2006, 11:14:56 AM »
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.

Glenn R

  • Guest
Re: c# reading file
« Reply #7 on: September 28, 2006, 07:08:04 PM »
In case anybody else is wondering as it doesn't seem Andrea will put up his solution:

Code: [Select]
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.