Jump to content

Using a Match Pattern VI


bob43

Recommended Posts

I am using Match Pattern to determine if a header is correct.

I read the file in and search for the regular expression.

The issue I am having is when I change the header by removing a character, when the header is rewritten as it is supposed to, but it decrements the next written character from the data.

I want to only rewrite the information on the first line (header) and not have any other portion of the file affected.

Link to comment

Hey Bob,

From what I can see there are several issues here.........

  1. The string that you are comparing to has '\t' as text inserted in the string not the code '\t' indicating a tab. To correct this, right click on the regular expression constant and select 'codes display'. You will then see some extra '\' inserted. Remove these (i.e. make it look like it did when it wasn't in code display mode).
  2. The reason you are losing a character is that the text you are writing is longer than the text you are replacing. Remember that you are writing text from the start of the file regardless of how may characters were read. If you read 5 characters and wrote 10, you would replace the first 5 that you intended plus an extra 5 that you didn't want to overwrite.

The only way around this is to read the text into memory after the matched string, append this to your new text and write it all out again.You can not just say 'replace the first line of text' because this requires other data in the file to be moved anyway.

I agree with Mikkel, you have already read all of the text into memory so instead of using the match pattern icon, whjy not use the search and replace icon which will do the replace for you and then write out the whole text again. I have attached what I mean.

Hope I have understood what you are trying to do!

Cheers

Download File:post-7495-1168703637.vi

Link to comment

If the above solutions are working for you, ignore the following advice. :)

Some people on LAVA have recommended that when you write a file that has both header and data, you start the data at the Nth character where N is a nice round number large enough to cover any possible header you would want there. If you really want to get fancy, you can pre-write the file with blank header and data before your main code executes, to speed execution (reduce time wasted on file space allocation). Note, I haven't tried these tricks yet, but they sounded smart to me.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.