Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > c# reading file
 

c# reading file

Hi all,

this is my first post here..

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 ?

thanks.

andrealisp  Wednesday, September 27, 2006 7:19 PM

sorry, what exactly are you trying to do/read? If the textfile shows | (the pipe symbol) it should read it...unless of course, its a different encoding?

have you tried to readToEnd() into a string to see if the text read contains the | symbol?

string theFile = file.ReadToEnd();

 

I've also noticed you have a counter in the code, it is not required and should be removed even though it has no effect in the processing of what you are trying to read.

you should also do a Peek() to see if there is data rather than the !=null condition as this would still throw a nullreferenceexception or an indexoutofrange exception:


string FILELINE;
          

// Read the file and display it line by line.
using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"))

{
   while ((file.Peek() > -1)

   {
      //System.Console.WriteLine(line);

      FILELINE = file.ReadLine();
      TAB1_listBox_file.Items.Add(FILELINE);

   }

}

ahmedilyas  Wednesday, September 27, 2006 8:09 PM
ahmedilyas wrote:

sorry, what exactly are you trying to do/read? If the textfile shows | (the pipe symbol) it should read it...unless of course, its a different encoding?

have you tried to readToEnd() into a string to see if the text read contains the | symbol?

string theFile = file.ReadToEnd();

I've also noticed you have a counter in the code, it is not required and should be removed even though it has no effect in the processing of what you are trying to read.

you should also do a Peek() to see if there is data rather than the !=null condition as this would still throw a nullreferenceexception or an indexoutofrange exception:


string FILELINE;

// Read the file and display it line by line.
using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"))

{
while ((file.Peek() > -1) //...ERROR HERE

{
//System.Console.WriteLine(line);

FILELINE = file.ReadLine();
TAB1_listBox_file.Items.Add(FILELINE);

}

}

Hi ahmedilyas, and thanks to aswer me.

like i said....i'm new to C#...in fact...i'm trying to make my first application.

Yes, i'm trying to read the file only and put all text between the " ¦ " caracter in a variable name.

don't know if this is the PIPE symbol but with my keyboard i can create-it by entering the "Alt+7 ".

I also try your code..but got an error (se in red).

andrealisp  Wednesday, September 27, 2006 8:31 PM

sure no worries. I believe I did not include the last closing bracket in the while loop, so it should be:

while ((file.Peek() > -1))

Now since you mention you are trying to put all the "chracter names" into a variable name after the | symbol, you can do this using a string split function, but would split the string/characters into a string array after finding the | symbol, acting as a "field" to split at. However let's get this one thing sorted before we move on....

ahmedilyas  Wednesday, September 27, 2006 8:40 PM

Hi

It seem to be Encoding problem. Try this:

System.IO.StreamReader file = new System.IO.StreamReader(@"test.txt", System.Text.Encoding.UTF7,false);

Yours

Markku

Markku Behm  Wednesday, September 27, 2006 10:17 PM

Hey !! It work !!

i have just replaced the @"test.txt" by @"c:\test.txt"

Thanks alot....

andrealisp  Thursday, September 28, 2006 1:24 PM
hi all i saw your solution and i have an assignment with the same idea but iam alittle confused about it .. may u can help me
this is the assignment
In this assignment you will required to build a sample form which reads its content from a text file. The format of the text file is as following:

Line1: will contain the Text property of the form

Line 2-to the end of the file: Each line will define a Control to be added to the form in the following format

Control Type, Text Property, X axis Location, Y axis Location

For example if the file contains:

Hello World

Text, Welcome, 10, 10

Button, Go, 100, 100

Label, Hi, 100, 200

Button, Click Here, 100, 100

This will show a form with Title “Hello World�with two buttons, one label, and one text box inside it.

flower89  Sunday, October 18, 2009 10:06 PM

You can use google to search for other answers

Custom Search

More Threads

• a datatable getting it's data from a .txt file
• Overwrite image in pictureBox Issue?
• Debugging Hosted COM DLL
• how to create class library in c#.net2005
• GetLastWriteTime doesn't work with image files?
• Need a Good Book
• how to edit a text file using c#?
• To Lock the keyboard
• Configuration manager help
• How to get data in a combobox