0

I want to write a Log File containing the names of all Files processed by the program.Im wondering what will be the right approach to do this

1.Array-Should I store the filenames in a string array and later write the array to text file.Will this result in memory wastage?

2.The second approach would be to create a file and keep the pointer to it open and write the result to it directly without using an intermediate array.But I don't know how to do this.

Currently I'm writing the file using

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

    let fileURL = dir.appendingPathComponent(file)

    //writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {/* error handling here */}

    //reading
    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)
    }
    catch {/* error handling here */}
}

UPDATE:

As per @Martin 's comment I have followed the following approach.But I'm getting some random characters in the output file

  //--------------Setting Up Logging and FileStream
       let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            .appendingPathComponent("log.txt")
        let logstream = OutputStream(url: fileURL, append: true)
        logstream?.open()
 //-------------------------------------
   writelog(stream: (logstream)!, fname: x.path)
   logstream.close()
 ----------------------------------

WriteLog Function

func writelog(stream:OutputStream,fname:String)
    {
        let text = fname
        let bytesWritten = stream.write(text, maxLength: 100)
        if bytesWritten < 0 { print("write failure") }
    }

Output:

/Users/me/Desktop/file1.jpg≠æ≠fifi¿›∫ÄZ+Ä`XTUM/Users/me/Desktop/file2.jpg≠æ≠fifi¿›∫Ä+JÄ`6@ø/Users/me/Desktop/test.jpgġˇˇˇˇˇÿ©ÔÕ´âˇˇˇˇ¿3©

Note: I had to set the Max Length attribute to 100 as the argument was required by the write function to work.

techno
  • 6,100
  • 16
  • 86
  • 192
  • Don’t store the file name store the resource url. – Leo Dabus Oct 22 '17 at 05:05
  • Btw you should use a plist file to store your urls – Leo Dabus Oct 22 '17 at 05:06
  • @LeoDabus I'm referring to storing user readable text files . – techno Oct 22 '17 at 05:29
  • What difference does it make the kind of file the url points to? You need to create an array with your urls and use NSKeyedArchiever to convert your array to data (property list file) and write your data (plist file) to disk. – Leo Dabus Oct 22 '17 at 05:33
  • https://stackoverflow.com/questions/26989493/how-to-open-file-and-append-a-string-in-it-swift shows how to *append* to an existing file. – Martin R Oct 22 '17 at 05:34
  • @MartinR He could use FileHandle also to append a string to the end of a text file. But I think he should use NSKeyedArchiever to convert his array to data and NSKeyedUnarchiever to load it from disk. I don't think he should write it to a text file – Leo Dabus Oct 22 '17 at 05:39
  • @MartinR Thanks ... This is what I was looking for. – techno Oct 22 '17 at 05:39
  • @techno You might want to take a look at this also if you really want to store it as text https://stackoverflow.com/a/46843717/2303865 – Leo Dabus Oct 22 '17 at 05:41
  • @LeoDabus: It depends. If the purpose is to write a *human readable log file* then a binary archive won't help. – Martin R Oct 22 '17 at 05:41
  • @MartinR if he doesn't need to convert it back to an array I agree a text file is enough – Leo Dabus Oct 22 '17 at 05:43
  • @LeoDabus I don't want to convert it back to an array. – techno Oct 22 '17 at 05:44
  • check the link I posted. You might think easier to use FIleHandle instead – Leo Dabus Oct 22 '17 at 05:44
  • @LeoDabus okay.Thanks – techno Oct 22 '17 at 05:45
  • @MartinR I followed your approach.I'm getting some random characters in the output file.Please see the update. – techno Oct 22 '17 at 07:15
  • @LeoDabus Please see the update. – techno Oct 22 '17 at 07:15
  • @techno: You are always writing 100 bytes, regardless of the string length, therefore garbage is written. Have a look at https://stackoverflow.com/a/26992040/1187415 again, it provides a `func write(_ string: String, ...)` method. – Martin R Oct 22 '17 at 07:36
  • @MartinR Thanks.. upon using the extension things work fine :) – techno Oct 22 '17 at 07:53

0 Answers0