Ask a questionAsk a question
 

Questionc# reading file

  • Wednesday, September 27, 2006 7:19 PMandrealisp Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

All Replies

  • Wednesday, September 27, 2006 8:09 PMahmedilyasMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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);

       }

    }

  • Wednesday, September 27, 2006 8:31 PMandrealisp Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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).

  • Wednesday, September 27, 2006 8:40 PMahmedilyasMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

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

  • Wednesday, September 27, 2006 10:17 PMMarkku Behm Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, September 28, 2006 1:24 PMandrealisp Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hey !!  It work !!

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

    Thanks alot....

  • Sunday, October 18, 2009 10:06 PMflower89 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.

  • Friday, November 06, 2009 11:14 AMDheeraj_Mallemala Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi, 

    here is code-snippet.



    private void InitializeForm() //Call this method in form's constructor
    {
        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))
            {
                FILELINE = file.ReadLine().Trim();
                if (FILELINE != string.Empty)
                    AddControlsAndSetPropeties(ref FILELINE);
            }
        }
    }

    private void AddControlsAndSetPropeties(ref string FILELINE)
    {
        string[] array = FILELINE.Split(new char[] { ',' });

        if (array.Length > 0)
        {
            switch (array[0].Trim().ToUpper())
            {
                case "TEXT":
                    TextBox textbox = new TextBox();
                    textbox.Name = array[1].Trim();
                    textbox.Text = array[1].Trim();
                    textbox.Top = Convert.ToInt32(array[2].Trim());
                    textbox.Left = Convert.ToInt32(array[3].Trim());
                    this.Controls.Add(textbox);
                    break;

                case "BUTTON":
                    Button button = new Button();
                    button.Name = array[1].Trim();
                    button.Text = array[1].Trim();
                    button.Top = Convert.ToInt32(array[2].Trim());
                    button.Left = Convert.ToInt32(array[3].Trim());
                    this.Controls.Add(button);
                    break;

                case "LABEL":
                    Label label = new Label();
                    label.Name = array[1].Trim();
                    label.Text = array[1].Trim();
                    label.Top = Convert.ToInt32(array[2].Trim());
                    label.Left = Convert.ToInt32(array[3].Trim());
                    this.Controls.Add(label);
                    break;

                default:
                    this.Text = array[0].Trim();
                    break;
            }
        }
    }