String Processing

New to FreeBASIC? Post your questions here.
storm5510
Posts: 49
Joined: Nov 28, 2019 15:47

String Processing

Postby storm5510 » Dec 01, 2019 16:20

I have been working with a larger program which includes a do..loop as shown below. Nothing inside the loop seems to work. The output file as created, but is always empty.

Code: Select all

dim as string Content
dim as string InputFile
dim as string OutputFile

open InputFile for input as #1
open OutputFile for append as #2

do until eof(1)
   line input #1, Content
   
   if left(Content,3) = "UID" then
      print #2, Content
      print left(Content,30) & ". . . ."
   end if
   
   if left(Content,1) = "{" then
      print #2, Content
      print left(Content,30) & ". . . ."
   end if
   sleep 250
loop

close #1
close #2


The input and output files are properly formatted valid names. The input lines are lengthy. Below is a sample.

Code: Select all

UID: storm5510/3570_Ivy_Bridge, M44587 completed....
{"exponent":"92893093", "worktype":"PM1"....


The top line is typically 150 characters long. The bottom, 265.

I am missing something, but I do not know what it may be.....
fxm
Posts: 9637
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: String Processing

Postby fxm » Dec 01, 2019 16:31

The string variable InputFile and OutputFile must be initialized with filenames.
counting_pine
Site Admin
Posts: 6200
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: String Processing

Postby counting_pine » Dec 01, 2019 17:08

You could try compiling with error checking (e.g. fbc -exx myfile.bas), see if that gives an error when the program runs.
Otherwise, try printing Content- or at least the first three characters of it, to the screen after Inputting it.
I don’t see any problems with the code, apart from the blank filenames, which I presume you’re setting to something.
I wonder if maybe the input file is in a Unicode encoding (e.g. UCS-2) or something, which would mean most every other byte is null.
Provoni
Posts: 378
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: String Processing

Postby Provoni » Dec 01, 2019 17:38

I had a similar problem and the input file was using the UTF-8-BOM encoding. You can check this with Notepad++ under Encoding. If so, put it to UTF-8.
srvaldez
Posts: 2429
Joined: Sep 25, 2005 21:54

Re: String Processing

Postby srvaldez » Dec 01, 2019 17:40

also, it's possible that the input string has one or more spaces before UID in which the test fails, in that case, use if left(Ltrim(Content),3) = "UID" then
dodicat
Posts: 6495
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: String Processing

Postby dodicat » Dec 01, 2019 18:23

It works perfectly here, so long as the file is ascii .

Code: Select all

 dim as string Content
dim as string InputFile="data.dat"
dim as string OutputFile ="tester.dat"

 #Include "file.bi"
Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub

Function loadfile(file as string) as String
   If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

dim as string s
dim as string acc
for n as long=1 to 5000
    var t=rnd
    if t<.3 then s="UID"+"   "+str(rnd*1000000)+chr(10)
    if t>.3 and t<.6 then s="{"+str(rnd*1000000)+"}"+chr(10)
    if t>.6 then s="NULL"+str(rnd*1000000)+chr(10)
    acc+=s
next
acc+="{Done}"

savefile(inputfile,acc)



open InputFile for input as #1
open OutputFile for append as #2

do until eof(1)
   line input #1, Content
   if left(Content,3) = "UID" then
      print #2, Content
      'print left(Content,30) & ". . . ."
   end if
   
   if left(Content,1) = "{" then
      print #2, Content
      'print left(Content,30) & ". . . ."
   end if
   'sleep 250
loop

close #1
close #2
print loadfile("tester.dat")
kill "tester.dat"
kill "data.dat"
print
print fileexists("tester.dat"),fileexists("data.dat")
sleep
storm5510
Posts: 49
Joined: Nov 28, 2019 15:47

Re: String Processing

Postby storm5510 » Dec 01, 2019 23:31

InputFile and OutputFile are assigned proper names. I just did not show this in my example. Sorry!

Some of the files are from a Linux system, so I use Notepad++ to change the coding. There are no leading spaces. I used a hex-editor to look for other characters.

I use -e when I compile. Also, there is an on error statement at the top. It never goes there. I have a message to display if it does. There is never any output to the screen despite the print statements.

I think this covers everyone's responses, except the last. Using the binary option seems to be the long way around. In this case, not my cup-of-tea, but I appreciate the effort.

I will keep experimenting...
paul doe
Posts: 1225
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: String Processing

Postby paul doe » Dec 02, 2019 4:34

Or, if you're running the code on Linux, check the casing of the file names. File names in *nix are case sensitive, and this also caused me some minor annoyances in the past...
dodicat
Posts: 6495
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: String Processing

Postby dodicat » Dec 02, 2019 11:10

storm5510 wrote:InputFile and OutputFile are assigned proper names. I just did not show this in my example. Sorry!
. . .
I think this covers everyone's responses, except the last. Using the binary option seems to be the long way around. In this case, not my cup-of-tea, but I appreciate the effort.

I will keep experimenting...

If you mean my post (the last response), then I am simply using your code (with the screen print commented out), The binary I use is in savefile and loadfile only, I could have used other ways to create a test file and view the results from your code (shell for example).
If my code (YOUR CODE) works with these files then your problem could be with a utf bom in a file, or as Paul doe suggested, the string case of a filename.
storm5510
Posts: 49
Joined: Nov 28, 2019 15:47

Re: String Processing

Postby storm5510 » Dec 02, 2019 13:10

Problem solved.

All I had to do was this:

Code: Select all

dim as string InputFile = "results.txt"
dim as string OutputFile = "ParseData-Hold.txt"

It runs fine on both platforms. I was trying to get an InputFile name from a command statement. That may have worked. I was also attempting to create an OutputFile name using a function which would pad the year, all four digits, and the month, two digits, on the back end of "ParseData-" followed by ".txt." An example would be "ParseData-201912.txt." I have a VB6 program which will do this.

As I wrote somewhere else, VB6 and Windows 10 do not want to cooperate at times so I want to get away from VB6 completely.

I want to thank each and every one of you who replied... :-)
dodicat
Posts: 6495
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: String Processing

Postby dodicat » Dec 02, 2019 18:07

If you use inputfile=command(1), then you have drag and drop.
You can format the outputfile name any way you like (within the realms of common sense).

Code: Select all

 #include "vbcompat.bi"
dim as string Content
dim as string InputFile="results.txt"
var x= format(now,"yyyymm")
dim as string OutputFile ="ParseData-"+x+".txt"

 #Include "file.bi"
Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub

Function loadfile(file as string) as String
   If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

dim as string s
dim as string acc
for n as long=1 to 20
    var t=rnd
    if t<.3 then s="UID"+"   "+str(rnd*1000000)+chr(10)
    if t>.3 and t<.6 then s="{"+str(rnd*1000000)+"}"+chr(10)
    if t>.6 then s="NULL"+str(rnd*1000000)+chr(10)
    acc+=s
next
acc+="{Done}"

savefile(inputfile,acc)


#define getout print  "file not found":sleep:end

if open(InputFile for input as #1)<>0 then getout
if open (OutputFile for append as #2)<>0 then getout

do until eof(1)
   line input #1, Content
   if left(Content,3) = "UID" then
      print #2, Content
     
   end if
   
   if left(Content,1) = "{" then
      print #2, Content
     
   end if

loop

close #1
close #2
print loadfile(outputfile)
print "Output filename is  ";outputfile
print "Check:"

shell "dir "+outputfile
kill outputfile
kill inputfile
print
print fileexists(outputfile),fileexists(inputfile)
sleep

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests