读取文本文件并从填充标题中获取标题字符串。 [英] Read a text file and grab a header string from the padded header.

查看:78
本文介绍了读取文本文件并从填充标题中获取标题字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我真的非常需要这里的帮助,所以要求那些大师。我创建了一个文本文件,其文件标题的长度为100。标题的100长度按如下方式划分为文本文件的标题。



NameFile fileCode Datefile Officercode - 这是标题

DFirstMessage9102 92129 sjdjshd- 29819281是1ui完成---第一条消息

FSecondMessage92129 sjdjshd- 29819281是1ui完成 - --2nd message



对于上面的,标题的字符长度为100.这个标题的详细信息如下所示,在代码提取中我们文件是创建。



writeline(NameFile.PadRight(20)&fileCode.PadRight(30)&Date.Now()。Tostring.PadRight(30) &(Officercode.PadRight(20))



现在我的问题是我想读取上面的文件并获取文件的日期whcih是来自的日期文件header,andl我也想检查在这种情况下每个消息是否以特定字母F开头。请注意每条消息都有每个消息的固定长度,如标题的长度,如1000。用于创建消息的代码提取也如下所示。



writeline(message1.PadRight(1000))

WriteLine(message2.Padright1000 )



请帮助我如何从文件标题中获取日期,并在消息开始之前检查每条消息是否有一个F infront。如果F存在则拔出消息,否则保留消息。所以对于这个,第二条消息只会被拔出。





提前谢谢

Hello,
I really need assistance badly here so asking the gurus out there. I have created a text file which has the file header which has a length of say 100. The 100 length for the header is divided as follows for the Header of the text file.

NameFile fileCode Datefile Officercode --This is the header
DFirstMessage9102 92129 sjdjshd- 29819281 is 1ui finish ---1st message
FSecondMessage92129 sjdjshd- 29819281 is 1ui finish ---2nd message

For the above the, header has a character length of 100. This is divided into the header details as follows in the code extract wehn the file was created.

writeline("NameFile".PadRight(20) & "fileCode".PadRight(30) & Date.Now().Tostring.PadRight(30) & ("Officercode".PadRight(20))

Now my issue is I want read the above file and grab the date for the file whcih is the Datefile from the header, andl also I want to check if each message starts with a specific letter F in this case. Note that each message has a fixed length as that of the header like 1000 for each message. code extract used to create the message is as follows too.

writeline(message1.PadRight(1000))
WriteLine(message2.Padright1000)

Please assist how I could get the date from the file header and check if each message has a F infront before the message starts. If F exists then pull out the message, otherwise leave the message. So for this the second message will be pulled out only.


Thanks in advance

推荐答案

这里有一些问题,这意味着您可能想重新考虑如何处理文件头。

首先是PadRight修复最小值字符串的长度,而不是最大值,所以如果你的文件名或代码分别超过20或30个字符,那么你的整个系统会崩溃,因为你无法分辨一个字段的结束和一个新字段的开始。



接下来是DateTime.ToString依赖于文化:它将根据用户为其PC选择的设置生成日期,因此您的文件可能是根本不可移植:在一台系统上写的文件在另一台PC上读取时会产生不同的日期(或错误)。



如果你想使用文本文件的文件标题行,然后使其变长,并将各个部分分开h文件名中不允许使用的字符:''\ t''或''?''。然后使用固定的数据格式,例如

There are a couple of problems here, which mean that perhaps you want to rethink how you are doing the file header.
The first is that PadRight fixes a minimum length for the string, not a maximum, so if your file name or code exceed 20 or 30 characters respectively, then your whole system goes to pieces, as you can''t tell where one field ends and a new one begins.

The next is that DateTime.ToString is culture dependant: it will generate a date based on the settings the user has selected for his PC, and so your files may not be at all portable: a file written on one system will produce a different date (or an error) when read on a different PC.

If you want to use a file header line for a text file, then make it variable length, and separate the various parts with a character that isn''t allowed in file names: ''\t'' or ''?'' for example. Then use a fixed data format such as
DateTime.Now.ToString("yyyyMMdd")

只需将其作为一行文字:

And just have it as a line of text:

20130523?myfile.txt?myCode?Ociffer

然后您可以非常轻松地检索线条,并使用简单的字符串获取各个部分。分割操作。

You can then retrieve the line very easily, and get the various parts with a simple string.Split operation.


这篇关于读取文本文件并从填充标题中获取标题字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆