none
文本文件读取错误,请帮忙! RRS feed

  • 问题


  • 我的文本是这样的:
    1 2 3 4
    2 4 6 8
    3 6 9 12
    4 8 12 16
    读取后生成另外一个文本,其内容又变成了这样;
    49 32 50 32
    51 32 52 32
    13 10 50 32
    52 32 54 32
    具体代码为:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Const path As String = "c:\mytest.txt"
            Dim i As Integer
            Dim j As Integer
            Dim file As StreamWriter
            file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
            For i = 1 To 4
                For j = 1 To 4
                    file.Write(i * j & " ")
                Next
                file.WriteLine()
            Next
            file.Close()
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Const path As String = "c:\mytest.txt"
            Dim i As Integer
            Dim j As Integer
            Dim a(4, 4) As String
            Dim file As StreamReader
            file = My.Computer.FileSystem.OpenTextFileReader(path, System.Text.Encoding.Default)
            For i = 1 To 4
                For j = 1 To 4
                    a(i, j) = Convert.ToDouble(file.Read())
                Next
            Next
            file.Close()
            Const path1 As String = "c:\mytest1.txt"
            Dim file1 As StreamWriter
            file1 = My.Computer.FileSystem.OpenTextFileWriter(path1, False)
            For i = 1 To 4
                For j = 1 To 4
                    file1.Write(Convert.ToString(a(i, j)) & " ")
                Next
                'file.Write(i * 4)
                file1.WriteLine()
            Next
            file1.Close()

        End Sub
    请各位帮忙,我想生成跟原文件一模一样的文件,应该怎么做?

    2010年4月17日 0:43

答案

  • Hi,

    请参考以下的代码

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const path As String = "c:\test\mytest.txt"
        Dim i As Integer
        Dim j As Integer
        Dim file As StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
        For i = 1 To 4
          For j = 1 To 4
            file.Write(i * j & " ")
          Next
          file.WriteLine()
        Next
        file.Close()
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Const path As String = "c:\test\mytest.txt"
        Dim i As Integer
        Dim j As Integer
        Dim a(3, 3) As String '只有4排
        Dim file As StreamReader
        file = My.Computer.FileSystem.OpenTextFileReader(path, System.Text.Encoding.Default)
        For i = 0 To 3
          Dim temp As String = file.ReadLine() '读一行然后用Split的方法把它分开
          For j = 0 To 3
            a(i, j) = temp.Split(" ")(j) '每列都有空格所以用空格分开
          Next
        Next
        file.Close()
        Const path1 As String = "c:\test\mytest1.txt"
        Dim file1 As StreamWriter
        file1 = My.Computer.FileSystem.OpenTextFileWriter(path1, False)
        For i = 0 To 3
          For j = 0 To 3
            file1.Write(a(i, j) & " ")
          Next
          'file.Write(i * 4)
          file1.WriteLine()
        Next
        file1.Close()
      End Sub

    • 已标记为答案 Kira Qian 2010年4月23日 9:15
    2010年4月17日 3:59

全部回复

  • Hi,

    请参考以下的代码

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const path As String = "c:\test\mytest.txt"
        Dim i As Integer
        Dim j As Integer
        Dim file As StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
        For i = 1 To 4
          For j = 1 To 4
            file.Write(i * j & " ")
          Next
          file.WriteLine()
        Next
        file.Close()
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Const path As String = "c:\test\mytest.txt"
        Dim i As Integer
        Dim j As Integer
        Dim a(3, 3) As String '只有4排
        Dim file As StreamReader
        file = My.Computer.FileSystem.OpenTextFileReader(path, System.Text.Encoding.Default)
        For i = 0 To 3
          Dim temp As String = file.ReadLine() '读一行然后用Split的方法把它分开
          For j = 0 To 3
            a(i, j) = temp.Split(" ")(j) '每列都有空格所以用空格分开
          Next
        Next
        file.Close()
        Const path1 As String = "c:\test\mytest1.txt"
        Dim file1 As StreamWriter
        file1 = My.Computer.FileSystem.OpenTextFileWriter(path1, False)
        For i = 0 To 3
          For j = 0 To 3
            file1.Write(a(i, j) & " ")
          Next
          'file.Write(i * 4)
          file1.WriteLine()
        Next
        file1.Close()
      End Sub

    • 已标记为答案 Kira Qian 2010年4月23日 9:15
    2010年4月17日 3:59
  • 谢谢你的热心回答,给了我帮助,我现在的疑问是为什么file.read()就不行呢?他不是指只读取一个字符么?

    我用其他的办法也成功了:

    读取部分为:

            Dim SRfile As TextFieldParser
            SRfile = My.Computer.FileSystem.OpenTextFieldParser(path, " ")
            Dim currentRow As String()
            For i = 1 To 4
                currentRow = SRfile.ReadFields()
                For j = 1 To 4
                    a(i, j) = currentRow(j - 1)
                Next
            Next
            SRfile.Close()

     

    我还是不习惯数组从0开始,请谅解!

    2010年4月17日 8:38
  • Hi,

    File.Read()只读一个字符,所以当你要的是多过一个字符如12或16的时候就会有问题。其它方面我想应该没问题。

    2010年4月17日 15:18
  • 你没发现吗,Read返回的是字符的asc码,"1"=0x31=49 空格=0x20=32

    写的时候要转换为字符

    file1.Write(chr(a(i, j)) & " ")


    http://feiyun0112.cnblogs.com/
    2010年4月21日 8:32
    版主