Trying to get started again . . .

New to FreeBASIC? Post your questions here.
Post Reply
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Trying to get started again . . .

Post by cavelamb »

I'm posting this in Beginners, because that's what it feels like.
Haven't written anything in a long while now.
I've probably forgotten more than I ever knew?

I'm trying to polish up a small file stripper that I started a few years ago, and I guess
still think in Quick 4.5.

I'm having problems getting a simple thing like Mid() working. (blush)



Code: Select all

'input file model
'   sometimes has a blank first line   
'00:00 
'transcript text from Youtube videos
'00:04 
'alternates a time hack line 
'00:06 
'then a text line
'00:08
'until end of file


'Declare Function Mid ( ByRef str as String, ByVal start as integer, ByVal n as integer ) as String
Dim txt As string
Dim chx As String
Dim go As String
Dim skip1 As Single
Dim cr1 As Single
Dim LinRed As Single
Dim LinOut As Single
Dim FileIn As String
Dim FileOut As String
Dim As String arg
Dim FileIs As Integer
Dim As Integer i = 1

    FileIn = Command(i)
    If Len(FileIn) = 0 Then
    	Print 
	 	Print "<Text File Stripper>"
	 	Print
    	Print "    No source file name given"
    	Print 
    	Print "    Strip /? for help"
    	Print
    	End
    End If
    
    Print "command line argument: ";  FileIn	 
	 
    If FileIn = "/?" Then
	  Print 
	  Print "<Text File Stripper>"
	  Print
          Print "   Written by Richard Lamb" 
          Print "   during the solar eclipse - 2017"
          Print
	  Print "   Strips time codes from Youtube transcripts"
	  Print "   alternating lines"
	  Print "   first line is time code"
          Print "   next line is text"
          Print
          Print "   Spaces not allowed in file names!"
          Print
          Print "   Output is written to 'filename'+'.out'"
          Print
          Print     
          End	 	 
   EndIf
	 
	 
   FileIs = Open (FileIn For Input As #1) 	
	 If FileIs = 0 Then ' file exists
	 	 Print "<Text File Stripper>"
	 	 Print	 	 
	 	 Print "    Input file: "; FileIn
	 	 Print
	 	 i = InStr(FileIn,".")
	 	 If i > 0 Then
	 	 	FileOut = Left(FileIn, i-1)+"-out.txt"
	 	 Else
	 	 	FileOut = FileIn + "-out.txt"
	 	 EndIf
	 	 	print
	      Print "    Output file: "; FileOut
	 	 	Print
	 	  	 
	 Else
	 	 Print "<Text File Stripper>"
	 	 Print	 	 
	 	 Print "    File Not Found: "; FileIn
                 Print 
                 Print
                 end
	 EndIf
	 

Open FileIn For input As #1
Open FileOut For Output As #2
  
  'see input file format model at beginning of this file
  
  While Not Eof(1)
	Line Input #1, txt
        chx = mid(txt,3,1)      ' looking for colon in time hack
        print chx,               	 ' not seeing anything yet . . .
       if chx = ":" then           ' skip time hack line
      
       else                      	 ' this part IS working
                                	 ' so the IF test is returning false.
         LinRed+=1
       	 put #2,, txt
   	     Put #2,,  " "
   	     Put #2,, Chr(13)
   	  end if					
  Wend
 
Close #1
Close #2

Print "read  "; LinRed; " Lines"
Print
Print "ended"
print

Print " cr to close program..."
Go = Input(1)
	
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Trying to get started again . . .

Post by srvaldez »

hello cavelamb
it might help if you would post a small sample of the file you are trying work on and what the result should be after processing and what results you are getting now
never bothered with YouTube transcripts before, so I tried and got a small sample, is this what your files look like?
00:10
(BIRDS CHIRPING)
00:33
(PANTING)
00:39
(BELL RINGING)
00:43
(TRAIN HORN BLOWING)
00:48
(PEOPLE CHATTERING)
01:32
(GRUNTING)
01:33
(PANTING)
01:45
Giddy up, we're almost home.
01:47
(LAUGHING)
01:53
(BIRDS CHIRPING)
02:16
(SCREAMING)
02:26
(LAUGHING) All right.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Trying to get started again . . .

Post by fxm »

Code: Select all

   FileIs = Open (FileIn For Input As #1)
   Close #1  '' <==== otherwise, file not closed but then reopened with the same file number !
    If FileIs = 0 Then ' file exists
        Print "<Text File Stripper>"
        Print       
        Print "    Input file: "; FileIn
        Print
        i = InStr(FileIn,".")
        If i > 0 Then
           FileOut = Left(FileIn, i-1)+"-out.txt"
        Else
           FileOut = FileIn + "-out.txt"
        EndIf
           print
         Print "    Output file: "; FileOut
           Print
            
    Else
        Print "<Text File Stripper>"
        Print       
        Print "    File Not Found: "; FileIn
                 Print
                 Print
                 end
    EndIf
Note:
To test the existence of a file, you can also use the FreeBASIC keyword 'FileExists' (+ #include "file.bi"), which avoids opening the file for that.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

srvaldez wrote:hello cavelamb
it might help if you would post a small sample of the file you are trying work on and what the result should be after processing and what results you are getting now
never bothered with YouTube transcripts before, so I tried and got a small sample, is this what your files look like?
00:10
(BIRDS CHIRPING)
00:33
(PANTING)

Yep.

My problem is working with the Freebasic syntax.

I know I could do it as QB code.

But I get lost without the FB version. (oh my)
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

Thanks to fmx..
Will do - as soon as I get this mid string silliness rolling.

fxm wrote:

Code: Select all

   FileIs = Open (FileIn For Input As #1)
   Close #1  '' <==== otherwise, file not closed but then reopened with the same file number !
    If FileIs = 0 Then ' file exists
        Print "<Text File Stripper>"
        Print       
        Print "    Input file: "; FileIn
        Print
        i = InStr(FileIn,".")
        If i > 0 Then
           FileOut = Left(FileIn, i-1)+"-out.txt"
        Else
           FileOut = FileIn + "-out.txt"
        EndIf
           print
         Print "    Output file: "; FileOut
           Print
            
    Else
        Print "<Text File Stripper>"
        Print       
        Print "    File Not Found: "; FileIn
                 Print
                 Print
                 end
    EndIf
Note:
To test the existence of a file, you can also use the FreeBASIC keyword 'FileExists' (+ #include "file.bi"), which avoids opening the file for that.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Trying to get started again . . .

Post by fxm »

This should work with my proposed change.
A third solution would be not to reopen the file:

Code: Select all

   FileIs = Open (FileIn For Input As #1)    
'   Close #1  '' <==== otherwise, file not closed but then reopened with the same file number !
    If FileIs = 0 Then ' file exists
        Print "<Text File Stripper>"
        Print       
        Print "    Input file: "; FileIn
        Print
        i = InStr(FileIn,".")
        If i > 0 Then
           FileOut = Left(FileIn, i-1)+"-out.txt"
        Else
           FileOut = FileIn + "-out.txt"
        EndIf
           print
         Print "    Output file: "; FileOut
           Print
            
    Else
        Print "<Text File Stripper>"
        Print       
        Print "    File Not Found: "; FileIn
                 Print
                 Print
                 end
    EndIf
   

'Open FileIn For input As #1
Open FileOut For Output As #2
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Trying to get started again . . .

Post by MrSwiss »

cavelamb wrote:Will do - as soon as I get this mid string silliness rolling.
It's all in the 'language reference' part of FB-documentation: FBWiki : KeyPgMidfunction
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

I seem to have regressed back to the larvae stage?

How did changing the test for the input file from "open" to "FileExist" make the mid(me$$) work???

Because it did.
Works like it was supposed to now.

(I hate when that happens!)

Code: Select all

#include "file.bi"

Dim txt As string
Dim chx As String
Dim go As String
Dim skip1 As Single
Dim cr1 As Single
Dim LinRed As Single
Dim LinOut As Single
Dim FileIn As String
Dim FileOut As String
Dim As String arg
Dim FileIs As Integer


Dim As Integer i = 1


    FileIn = Command(i)
    If Len(FileIn) = 0 Then
    	Print 
	 	Print "<Text File Stripper>"
	 	Print
    	Print "    No source file name given"
    	Print 
    	Print "    Strip /? for help"
    	Print
    	End
    End If
    
    Print "command line argument: ";  FileIn	 
	 
    If FileIn = "/?" Then
	  Print 
	  Print "<Text File Stripper>"
	  Print
      Print "   Written by Richard Lamb" 
      Print "   during the solar eclipse - 2017"
      Print
	  Print "   Strips time codes from Youtube transcripts"
	  Print "   alternating lines"
	  Print "   first line is time code"
      Print "   next line is text"
      Print
      Print "   Spaces not allowed in file names!"
      Print
      Print "   Output is written to 'filename'+'.out'"
      Print
      Print     
      End	 	 
	 EndIf
	 
	 If fileExists(FileIn) Then ' file exists
	 	 Print "<Text File Stripper>"
	 	 Print	 	 
	 	 Print "    Input file: "; FileIn
	 	 Print
	 	 i = InStr(FileIn,".")
	 	 If i > 0 Then
	 	 	FileOut = Left(FileIn, i-1)+"-out.txt"
	 	 Else
	 	 	FileOut = FileIn + "-out.txt"
	 	 EndIf
	 	 	print
	      Print "    Output file: "; FileOut
	 	 	Print
	 	  	 
	 Else
	 	 Print "<Text File Stripper>"
	 	 Print	 	 
	 	 Print "    File Not Found: "; FileIn
       Print 
       Print
       
       end
       
	 EndIf
	 

Open FileIn For input As #1
Open FileOut For Output As #2
  
  While Not Eof(1)
	Line Input #1, txt
      chx = mid(txt,3,1)        'looking for colon in time hack

      if chx = ":" then         'skip time hack line
      
      else                      ' this part IS working
                                ' so the IF test is returning false.
         LinRed+=1
       	 put #2,, txt
   	     Put #2,,  " "
   	     Put #2,, Chr(13)
   	  
      end if					
  Wend
 
Close #1
Close #2

Print "read  "; LinRed; " Lines"
Print
Print "ended"
print

	
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Trying to get started again . . .

Post by fxm »

Because reopen with the same file number a file already opened induces a runtime error at the line 'Open FileIn For input As #1':
Aborting due to runtime error 1 (illegal function call) at line 88 of C:\.....
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

@fmx...
Smarty pants.
SORTA Makes sense now.

But the original program did run correctly...
Opened the input file (twice, as you pointed out) and read it correctly.

If I manually skipped time hack lines it made the correct output.

Line Input #1, cod
Line Input #1, txt

So it DID read from the input file -
It just couldn't do the mid(me$) to catch the third character in the line.

But it read the line (correctly)and wrote it correctly.

That's kinda confusing...

But I went back and tried the original but commented out the second OPEN FileIn line.
And suddenly it makes happy mid(me$) tests.
So no question that you are correct here.

But why was it reading from a file that had been aborted?

'cuz I don't see that runtime error message here.

Also...
The input file sometimes has a blank line at the beginning.
That throws everything out of kilter, of course, but I can fix that now.
Thanks again.
Last edited by cavelamb on Nov 28, 2020 2:46, edited 1 time in total.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

Anyway, thanks for helping me see the light.

As it stands now, dressed up a bit for Windows...

Code: Select all

#define WIN_INCLUDEALL
#include once "windows.bi"

dim InFileName as String
dim TxtLine(4096) as string
dim TxtCnt as Integer
Dim txt As string
Dim chx As String
Dim cr1 As Single
Dim LinRed As Single
Dim LinOut As Single
Dim FileIn As String
Dim FileOut As String
Dim As Integer i = 1

TxtCnt = 0

Function filebrowser (Byref ititle As String, Byref idir As String = Curdir) As String
  Dim ofn As OPENFILENAME
  Dim filename As Zstring * (MAX_PATH + 1)
  Dim title As Zstring * 32 => ititle
  Dim initialdir As Zstring * 256 => idir
 
  With ofn
    .lStructSize       = Sizeof(OPENFILENAME)
    .hwndOwner         = NULL
    .hInstance         = GetModuleHandle(NULL)     
    .lpstrFilter       = Strptr(!"All Files, (*.*)\0*.*\0Bas Files, (*.txt)\0*.txt\0\0")
    .lpstrCustomFilter = NULL
    .nMaxCustFilter    = 0
    .nFilterIndex      = 1
    .lpstrFile         = @filename
    .nMaxFile          = Sizeof(filename)
    .lpstrFileTitle    = NULL
    .nMaxFileTitle     = 0
    .lpstrInitialDir   = @initialdir
    .lpstrTitle        = @title
    .Flags             = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_PATHMUSTEXIST
    .nFileOffset       = 0
    .nFileExtension    = 0
    .lpstrDefExt       = NULL
    .lCustData         = 0
    .lpfnHook          = NULL
    .lpTemplateName    = NULL
  End With

  If (GetOpenFileName(@ofn) = FALSE) Then Return ""
  Return filename
  
End Function

  FileIn = filebrowser("Youtube Transcript Stripper", "C:\")

  i = InStr(FileIn,".")
  If i > 0 Then
     FileOut = Left(FileIn, i-1)+"-out.txt"
  Else
	 FileOut = FileIn + "-out.txt"
  EndIf
  CLS
  Print
  print "  Input from file: "; FileIn
  print "  Outputs to file: "; FileOut 
  print
  print "========================================================================"
  print

Open FileIn For input As #1
Open FileOut For Output As #2
  
  While Not Eof(1)

    Line Input #1, txt
    
    chx = mid(txt,3,1)        'looking for colon in time hack
             
    if chx = ":" then   
        ' skip time hack 
    else                
       LinRed+=1
       put #2,, txt
       put #2, ,  " "
       Put #2,, Chr(13)
       TxtCnt = TxtCnt+1
       TxtLine(TxtCnt) = txt
    end if  
  Wend
 
Close #1
Close #2
'  print the output array to the console
'Cls
for i = 1 to TxtCnt
   print using " #### - ";i; 
   print TxtLine(i)
Next i

Sleep

Close
end
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Trying to get started again . . .

Post by BasicCoder2 »

cavelamb wrote:@fmx...
Smarty pants.
Although I read that as being meant in a nice way keep in mind it usually means someone who displays intelligence in an annoying way.
fxm has been here for a long time helping people with the FreeBasic language and is much appreciated.
Last edited by BasicCoder2 on Nov 28, 2020 7:59, edited 1 time in total.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Trying to get started again . . .

Post by fxm »

Not fmx (Freestyle motocross), but fxm.
cavelamb
Posts: 52
Joined: Jan 04, 2010 9:03
Location: earth

Re: Trying to get started again . . .

Post by cavelamb »

He (fxm) is indeed much appreciated.
Post Reply