当我尝试将记录保存到文件时,我一直收到错误的记录长度错误
任何人都可以给我任何建议吗?查看代码段。
我也需要将此方法用作类。
感谢
Code snippet
Dim output As String
'Old Delete Block
If File.Exists(fileName) = True Then
File.Delete(fileName)
Debug.WriteLine("file deleted ")
End If
'Using fs As FileStream = File.Create(fileName)
' For Each t In coastalTowns
' output = New UTF8Encoding(True).GetBytes(String.Format(myFormat, t.name, t.county, t.population, t.area))
' fs.Write(output, 0, output.Length)
' Next
'End Using
FileOpen(2, fileName, OpenMode.Random, , , 43)
Debug.WriteLine(Len(town))
While EOF(2)
For Each T In coastalTowns
If Not T.name = "" Then
output = (String.Format(myFormat, T.name, T.county, T.population, T.area))
FilePut(2, output)
End If
Next
End While
我确实理解这是一个过时的写入文件,但我们为类提供的数据文件需要使用此方法。 这是Structure和myFormat声明
Structure TownType
<VBFixedString(20)> Dim name As String
<VBFixedString(15)> Dim county As String
Dim population As Integer
Dim area As Integer
End Structure
Dim town As TownType
Dim myFormat As String = "{0, -20}{1,-20}{2,-10}{3,-10}"
这是我不断得到的错误,我已阅读 FilePut documentation
System.IO.IOException was unhandled
HResult=-2146232800
Message=Bad record length.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.FileSystem.FilePut(Int32 FileNumber, String Value, Int64 RecordNumber, Boolean StringIsFixedLength)
at task_1.Module1.saveFile() in J:\AS Comp Linear\Pract\VB.Net\Sect 2 Prog Tasks NT Bk\Section 2 - Prog\2.7 Text files and files of records\task 1\2.7-Task_2\task 1\Module1.vb:line 83
at task_1.Module1.Main() in J:\AS Comp Linear\Pract\VB.Net\Sect 2 Prog Tasks NT Bk\Section 2 - Prog\2.7 Text files and files of records\task 1\2.7-Task_2\task 1\Module1.vb:line 116
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: