locked
How to read each line in a file RRS feed

  • 問題

  • If there is a txt file, how to read the data each line and determine "4" is an integer, "a" is a character???

    Example, in the txt file contains:

    4

    a

    6

    8

    2

     

    2008年11月1日 16:29

解答

  • If in this case, after you can:

    1. Use StreamReader (or other reader objects) to open the file

    2. Use the ReadLine method in the Reader object to read line by line

    3. Use the TryParse shared method in the Integer class to determine whether it is an integer and get the value if it is

    4. If the TryParse method return False, then use the TryParse shared method in the Char class to determine whether it is an integer and get the value if it is.  Then check whether the returned value is between "a" and "z" or "A" and "Z".

    5. Close the Reader object.

     

    Note that the above is one possible solution only and is not optimized.  You may need to handle any exception raised in the code.

     

    2008年11月1日 17:13
  • How about reading each line and put it in a string collection first?  After that, you can get random access to the collection, e.g. if you would like to get the fifth line, then you can retrieve the line content by stringCollection(4).  Once you got the line content, you can apply your checking routine for integer and character.

    2008年11月1日 17:25

所有回覆

  • Before giving you any advise, you have to clarify the follow first:

    1. Is that only 1 character on each line?

    2. Can "4" be determined as a character?

    3. Is that "character" not include symbols, e.g. /?'";:!@#$%^&*(), etc.?

    2008年11月1日 17:00
  • yes, 1 integer or character each line.

    1,2,3,4,5,0,-1,1000 are integers

    a,b,g,F,E,I,Y are characters

    /?'";:!@#$%^&*() are not contained in the file.

    2008年11月1日 17:03
  • If in this case, after you can:

    1. Use StreamReader (or other reader objects) to open the file

    2. Use the ReadLine method in the Reader object to read line by line

    3. Use the TryParse shared method in the Integer class to determine whether it is an integer and get the value if it is

    4. If the TryParse method return False, then use the TryParse shared method in the Char class to determine whether it is an integer and get the value if it is.  Then check whether the returned value is between "a" and "z" or "A" and "Z".

    5. Close the Reader object.

     

    Note that the above is one possible solution only and is not optimized.  You may need to handle any exception raised in the code.

     

    2008年11月1日 17:13
  • I want to get the fifth line or second line or fourth line of the data in the txt file, my code is like this:

    Dim objectReader As New System.IO.StreamReader("C:\number.txt")
                Do While objectReader.Peek() <> -1
                output = Integer.Parse(objectReader.ReadLine())
                Console.WriteLine(output)

     

    My txt file contains:

    3

    5

    9

    10

    4

     

    I can only show all the context of the file but not the fifth line I want to display, how to get the fifth line or second line or fourth line???

     

    2008年11月1日 17:19
  • How about reading each line and put it in a string collection first?  After that, you can get random access to the collection, e.g. if you would like to get the fifth line, then you can retrieve the line content by stringCollection(4).  Once you got the line content, you can apply your checking routine for integer and character.

    2008年11月1日 17:25