Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 23-May-13 0:50am    
Don't do it at all. Padding, hard-coded length of strings. Better explain what do you want to achieve, in terms of data and its purpose, not silly formatting of who-knows-what...
—SA
ekipongi 23-May-13 0:55am    
Thanks _SA for the reply anyway. I'm just showing how the text file has been created from teh code extracts above. The length for the Header is 100 which is padded for each header details to cover the 100 length given. My issue is How do I read such a file and grab the Datefile from the header.

1 solution

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
C#
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.
 
Share this answer
 
Comments
ekipongi 23-May-13 1:46am    
Thanks OrigionalGriff. The above padded sample file was sent to me. The code extracts above I posted is to clarify the question here. What I want to do is grab the date from the header in the above file sent to me. how could I do that? Please assist with any code sample to grab the date which starts at the 31 position and finishes at the 80 position?
OriginalGriff 23-May-13 2:04am    
Um.
Have you looked at the string.Substring method?
http://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.71).aspx
ekipongi 23-May-13 2:21am    
seen the substring method but not really sure how I'd use that method to grab the date from the header. really need a help in the code to get this thing working. Thanks.
OriginalGriff 23-May-13 2:32am    
Read the header as a string - you know how to do that, I assume?
Then just call substring, giving it the index of the first character, and the number of characters you want.
So if your string is
MyFilename20130523Dibble
Then headerstring.Substring(10,8) will give you the eight characters starting at the eleventh (indexes are zero based)
Or "20130523"
ekipongi 23-May-13 2:45am    
Thank you so much OrigionalGriff. Got working but I'm stuck on the message bit as highlighted in the initial question posted above. How to check if F exists for each message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900