Jump to content

Simple FileReadLine problem


Recommended Posts

My goal is to parse a source text file and if any lines contain a hyphen then write that line to a new txt file. So the resulting product file will be a subset of the first file, but only lines that contain a hyphen.

Here's what I got:

$input = FileOpen("C:tempsource.txt", 0)
$output = FileOpen("C:tempdone.txt", 2)

Do
    $line = FileReadLine($input)
    If StringInStr($line, "-") Then FileWriteLine($output, $line)
Until @error = -1
    
FileClose($input)
FileClose($output)

Problem is, my resulting file only contains the first instance of a line with a hypen, which is repeated over and over again. It's as if the script isnt moving to the next line the 2nd time it executes FileReadLine. The documentation says FileReadLine should automatically increment and read the next line in an opened file, so there shouldn't be any need to set up a counter to guide it.

Any ideas why I'm getting this problem then? What simple thing am I missing? Much TiA.

Edited by club
Link to comment
Share on other sites

Ok, maybe not as expected. Seems the script freezes consistantly at some point. I thought it was hitting some character that was freaking it out but that doesn't seem to be the case.

For example, if I open the output txt file it seems the script froze in mid-write. It partially wrote a line it found a hyphen in, but the line abruptly ends when there was more to that line in the input file.

But then it gets even weirder. If I right click the script's icon and force it to exit, the output file instantly becomes complete. So it was working, but like stuck somehow. All those FileWriteLine's were like stuck in memory or something. And why would they suddenly work when I force the script to stop. Doesnt that just end the script immediately and dump whatever it was working on in memory?

I mean, I guess it's working in a roundabout way... I wait till it seems to be frozen and then manually stop it and the output is as desired. But what is going on here? I've added a Sleep in my loop to see if that would help, but it seems to make no difference.

Edited by club
Link to comment
Share on other sites

  • Moderators

club,

I see a major problem with that script - you are checking for @error for the wrong function. You want to trap it after the FileReadLine return - at the moment you are checking the FileWriteLine return. :oops:

This works for me:

$input = FileOpen("source.txt", 0)
$output = FileOpen("done.txt", 2)

While 1
    $line = FileReadLine($input)
    If @error = -1 Then Exitloop
    If StringInStr($line, "-") Then FileWriteLine($output, $line)
Wend

FileClose($input)
FileClose($output)

That might also have something to do with your later problem - although I am not sure why. :bye:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I see now when I force the script the exit it doesnt just drop everything and die. It must do some housekeeping, such as close any opened files. This must be while my output file suddenly gets filled out as it should when I exit the script. It wasnt that the script was "freezing", just failing to exit the loop.

Well definitely I appreciate your input. But I'm so OCD and my college instructors hammered into my brain that forcing a break out of a loop instead of using the loop's specified exit conditions being satisfied is a no-no. Or at least, I wouldve gotten dinged for such, gradewise.

Edited by club
Link to comment
Share on other sites

  • Moderators

club,

forcing a break out of a loop instead of using the loop's specified exit conditions being satisfied is a no-no

Then do it this way: :oops:

Do
    $line = FileReadLine($input)
    ; Save the @error returned from this function
    $iError = @error
    If StringInStr($line, "-") Then FileWriteLine($output, $line)
Until $iError = -1

It is functionally identical - but I would criticize this code for using an unnecessary variable. :bye:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

A slight variation on the example Melba23 gave you.

$input = FileOpen("source.txt", 0)
$output = FileOpen("done.txt", 2)

$line = FileReadLine($input)
While Not @error
    If StringInStr($line, "-") Then FileWriteLine($output, $line)
    $line = FileReadLine($input)
Wend

FileClose($input)
FileClose($output)

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...