Writing text files

I am having trouble writing successive text files out of my program. It was working but now crashes with I/O error -1407 on the 3rd file and it doesn’t matter which order I do them in. It is an old program and I noticed I originally used fs.CreatTextFile so I changed that to TextOutputStream.Create(fs). I reuse fs each time

Suggestions

  dlg=New SaveAsDialog                        
  dlg.initialDirectory = fs                     
  dlg.Filter = "text/Html"
  dlg.promptText="Choose a location to save your Web SOLUTION." 
  dlg.SuggestedFileName="WebPageSol.html"   
  dlg.Title="Web Page Location"                           
  fs=dlg.ShowModalwithin(MainWindow1)  
  
  if fs <> Nil then
    tout = TextOutputStream.Create(fs)
    fs.MacCreator = "html"
    solution = 1
    tout.writeline "<HTML>"
    tout.writeline "<HEAD>"
    etc
    tout.close

Do you get a crash or an exception? When does the “crash” occur? Beginning or end of writing?

Have you researched the error? Have you tried to write everything at once?

Hi Beatrix,
I get an I/O Exception, at the beginning of writing.

I looked online for -1407 but could find anything, I wasn’t sure if that is a XOJO or an Apple error code.

I tried allocating a seperate separate folder variable to each file but that didn’t work.

errFSNotAFolder               = -1407, /* Expected a folder, got a file *

For future reference, you can find a list of macOS error codes at:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacErrors.h

I think that you’re confusing things here. On the 2nd line, you use “fs” as a folder, but then you use it as a file.

[quote=402730:@Martin Fitzgibbons]dlg=New SaveAsDialog
dlg.initialDirectory = fs
fs=dlg.ShowModalwithin(MainWindow1)

tout = TextOutputStream.Create(fs)
[/quote]

I would suggest using two separate variables. “targetFile” and “lastFolder”. Once you’ve finished writing the file you create “lastFolder” by storing targetFile.parent.

[code]dlg=New SaveAsDialog
dlg.initialDirectory = lastFolder
dlg.Filter = “text/html”
dlg.promptText=“Choose a location to save your Web SOLUTION.”
dlg.SuggestedFileName=“WebPageSol.html”
dlg.Title=“Web Page Location”
targetFile=dlg.ShowModalwithin(MainWindow1)

if targetFile <> Nil then
tout = TextOutputStream.Create(targetFile)
// fs.MacCreator = “html” — This is no longer needed.
solution = 1
tout.writeline “”
tout.writeline “”
etc
tout.close
lastFolder = targetFile.parent[/code]

Thanks Sam, that’s an awesome clean up. As I said an old program where I am probably introducing a few errors as I try and update it.
Cheers

Martin:

you can start (the next time) by reading the LR. For today’s example:

http://documentation.xojo.com/index.php/TextOutputStream

Read especifically the example and notice the Try / Catch / End Try lines…

It will be of Great Help. Cheers !