Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. LineInput LineFeed problem

LineInput LineFeed problem

Scheduled Pinned Locked Moved Visual Basic
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MohammadAmiry
    wrote on last edited by
    #1

    Hi I have a very large file in whose format is like this: KEYWORD SomeInformationLines KEYWORD SomeInformationLines I need to pass through the file and save the position of each keyword, so that when later, user wants me to read the keyword data from the file, I can seek to the position of the keyword and read the data. The problem is some files are unix format (i.e. their lines end with linefeed, Chr(10), only rather than CR-LF, Chr(13)+Chr(10)). This is My Code:

    	FileOpen(1,FileName)
    	
    	Do Until EOF(1)
    		strLine = LineInput(1)
    		If IsKeyword(strLine) Then
    			SaveKeywordPosition( strLine, Seek(1) )
    			' The above line is where the bug comes from:
    			' e.g if the line is empty, and only an LF is in it, Seek(1) returns 2!!
    			' i.e. it virtually adds a CR and this is done for all further lines too.
    			' But whem I use Seek(1, aPreviouslySavedPosition) it goes exactly to where
    			' it should go, without accounting for extra CRs which it added while reading
                            ' the file.
    		End If
    	Loop
    	
    	FileClose(1)
    

    I also have tried to with InputString function, StreamReader and FileStream classes and check the last character of the line manually, but the IO was raises from 800KB/s to 18MB/s and runtime from 15sec to 2min. Could someone please tell me how should I handle this?!

    D 1 Reply Last reply
    0
    • M MohammadAmiry

      Hi I have a very large file in whose format is like this: KEYWORD SomeInformationLines KEYWORD SomeInformationLines I need to pass through the file and save the position of each keyword, so that when later, user wants me to read the keyword data from the file, I can seek to the position of the keyword and read the data. The problem is some files are unix format (i.e. their lines end with linefeed, Chr(10), only rather than CR-LF, Chr(13)+Chr(10)). This is My Code:

      	FileOpen(1,FileName)
      	
      	Do Until EOF(1)
      		strLine = LineInput(1)
      		If IsKeyword(strLine) Then
      			SaveKeywordPosition( strLine, Seek(1) )
      			' The above line is where the bug comes from:
      			' e.g if the line is empty, and only an LF is in it, Seek(1) returns 2!!
      			' i.e. it virtually adds a CR and this is done for all further lines too.
      			' But whem I use Seek(1, aPreviouslySavedPosition) it goes exactly to where
      			' it should go, without accounting for extra CRs which it added while reading
                              ' the file.
      		End If
      	Loop
      	
      	FileClose(1)
      

      I also have tried to with InputString function, StreamReader and FileStream classes and check the last character of the line manually, but the IO was raises from 800KB/s to 18MB/s and runtime from 15sec to 2min. Could someone please tell me how should I handle this?!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      MohammadAmiry wrote:

      I need to pass through the file and save the position of each keyword, so that when later, user wants me to read the keyword data from the file, I can seek to the position of the keyword and read the data.

      OK. If you're using ReadLine to read the file, you won't get the LF character at the end of each line in the resulting string. ReadLine will strip the line termination characters for you. So, you simply have to add one to the character count of each line you read when you go to find each keyword. If you have a mix of file types, then you'll have to find out what the line termination characters are. Simply read the beginning of the file, character-by-character until you find either character 10 or 13, then check value of the next character to see if it is the opposite, 13 or 10, or if neither. This will tell you if the line termination is either CrLf, LfCr, or just Cr or Lf. You can adjust your character count accordingly.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups