notepad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siddhanta
    New Member
    • Oct 2009
    • 33

    notepad

    hi,
    in my notepad i have 2 rows of data seperated by spacebar. value of first row should be saved in one variable and the other in another variable. so, how to differentiate between this values as they are in the same line.
    any suggestion plz....
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    use string.split to split the line into to elements of a string array - using the space as the divider character

    Comment

    • siddhanta
      New Member
      • Oct 2009
      • 33

      #3
      notepad

      hi,
      how to take a value from notepad and save it in any variable.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        From the notepad.exe or from a text file on the hard drive?

        Comment

        • siddhanta
          New Member
          • Oct 2009
          • 33

          #5
          i have a .dat file where the values are save. i want to store this values one by one in variables.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            MSDN on how to read a text file:
            In this article, see examples of how to read text synchronously or asynchronously from a text file, using the StreamReader class in .NET for desktop apps.

            Comment

            • siddhanta
              New Member
              • Oct 2009
              • 33

              #7
              split value

              hi,
              i used the following code but getting problem with it.
              wat i wanna do is to store a line from notepad having two float values seperated by Space.
              private void button1_Click(o bject sender, EventArgs e)
              {
              StreamReader sw = new StreamReader("c : \\key.txt");
              line = sw.ReadLine();
              Console.WriteLi ne(line);//its showing the output
              string[] variables = line.Split(' ');
              float fvalue = float.Parse(var iables[0]);//this line is showing error while runnig.
              can u help plz..

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Code:
                string[] variables = line.Split(' '); 
                float fvalue = float.Parse(variables[0]);//this line is showing error
                What is the actual error?

                A. This assumes that the split returned something. If the split failed to find a space then 'variables' never gets values and remains null.
                B. This assumes that the first element of the array can be made into a float. Again, could return an error or at least unexpected results.

                Comment

                • siddhanta
                  New Member
                  • Oct 2009
                  • 33

                  #9
                  read notepad

                  hi,
                  to read from a notepad i hv used the following code.
                  bt the problem is, it will not read the next line.
                  so how to read line by line.....
                  Code:
                  using (StreamReader sr = new StreamReader("c: \\key.txt"))
                  {
                  
                    string s = sr.ReadLine();
                    //etc.
                    Console.WriteLine(s);//its showing the output
                  
                    string[] variables = s.Split(' ');
                    float fvalue = float.Parse(variables[0]);
                    Console.WriteLine(fvalue);
                  }
                  Last edited by Frinavale; Dec 9 '09, 08:49 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    See the examples at the MSDN docs page for this method.

                    You need to iterate over the file.

                    Mark.

                    Comment

                    • siddhanta
                      New Member
                      • Oct 2009
                      • 33

                      #11
                      split function

                      hi,
                      to seperate two integers in a line seperated by space i used the following function:
                      string[] variables = s.Split(' ');
                      but the problem is integers are seperated by more than one space, where the above functiion gives error. Also i cant use more than one space in split.
                      so, can u help plzz.....

                      Comment

                      • tlhintoq
                        Recognized Expert Specialist
                        • Mar 2008
                        • 3532

                        #12
                        Once you have the string[], trim the leading spaces..

                        MyArray[0] = MyArray[0].TrimStart();

                        Comment

                        • Curtis Rutland
                          Recognized Expert Specialist
                          • Apr 2008
                          • 3264

                          #13
                          Code:
                          string input = "1   2";  //notice that there are three spaces between the two ints
                          string[] delims = { " " };  //note that that's a single space
                          string[] tokens = input.Split(delims, StringSplitOptions.RemoveEmptyEntries);
                          tokens will now hold 2 strings, with values of "1" and "2". The "StringSplitOpt ions.RemoveEmpt yEntries" part discards empty tokens.

                          Comment

                          • Curtis Rutland
                            Recognized Expert Specialist
                            • Apr 2008
                            • 3264

                            #14
                            I like to use sr.ReadToEnd() if it's a relatively short text file. It will read the entire file into one string variable. If you want it broken into a string[] separated by newlines, you can .Split on the Environment.New Line value.

                            Comment

                            Working...