0

How can I write a string to a binary file? I have tried a lot, but every time my file appear as a plain text file..

I am not interested in a plist file..

            string = String(data: data, encoding: String.Encoding.utf8)!;
            var buffer = [UInt8](string.utf8);

            var f = fopen("newdata.dat", "w");
            let success = fwrite(buffer, MemoryLayout<UInt8>.size, buffer.count, f);
            fsync(fileno(f));
            fclose(f);
            print("success: \(success)");
Hamish
  • 78,605
  • 19
  • 187
  • 280
user3390652
  • 132
  • 1
  • 13
  • Might [this](http://stackoverflow.com/a/33814414/4041795) answer help you out maybe? – Montmons Apr 02 '17 at 12:59
  • 1
    What is wrong to write `data` directly to disk? – vadian Apr 02 '17 at 13:01
  • @SB87 Thanks, but unfortunately I have tried this before I post the question. Also I have specified that I am not interested in a plist file.. – user3390652 Apr 02 '17 at 13:04
  • @vadian I just want to learn something different. – user3390652 Apr 02 '17 at 13:07
  • Why do you convert `data` to a string and back to data? That fails for arbitrary data. Just `data.write(url: ...)` – Martin R Apr 02 '17 at 13:13
  • And what do you mean by *"my file appear as a plain text file"*? What result do you get and what do you expect? – Martin R Apr 02 '17 at 13:20
  • @Martin R for example I have a string="ABCDEF", and my output file contains "ABCDEF", but I am expecting something like "0a000" – user3390652 Apr 02 '17 at 13:31
  • 1
    How is "0a000" related to "ABCDEF"? What *exact* result do you expect for this string? – If your intention is to write a *hexadecimal representation* of the data, have a look at http://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift – Martin R Apr 02 '17 at 13:53
  • @Martin R Thank you. I am also reading the link you've attached, but I am struggling with map` { String(format: "%02hhx", $0) }.joined()` Would you mind explaining what is the different between `%02hhx` and `%02X`? – user3390652 Apr 02 '17 at 13:59
  • @user3390652: I tried to explain the `hh` modifier in my answer (and why you can omit it). – But *this* question is still unclear to me. – Martin R Apr 02 '17 at 14:01
  • @MartinR because I think everything is clear for me now :) Thanks!! – user3390652 Apr 02 '17 at 14:11

1 Answers1

0

Are you clear on the difference between data and text? A plain-text file is data that when interpreted by a text editor is displayed as text. Are you expecting to see zeroes and ones or an array of numbers (hexadecimal perhaps)? Or the kind of jumble of characters you see if you try and open a Word doc as plain text? If so then you need to open the file in an app like More Info

Or, the DIY option is to view the byte buffer but the only way you are going to view the bytes in a text editor is to transform them into a string and save this, which will represent the values of the original file but ironically not of the file itself (if that makes sense).

sketchyTech
  • 5,746
  • 1
  • 33
  • 56
  • please see my comments above :) – user3390652 Apr 02 '17 at 13:46
  • 1
    When you write that the file appears as a plain text file, how are you viewing that file and how are you loading it? Any file whether or not it is a type of plain-text encoding can be loaded as data. It has nothing to do with how you save the file. A plain-text file saved directly to disk is identical to the same file saved through the Data class, or as a "binary" file. All files are saved in binary format. The reason we call some docs binaries is because they cannot be interpreted in a human readable way in a text editor because of their complex encoding. – sketchyTech Apr 02 '17 at 13:53
  • 1
    Your answer has helped me figure out what is wrong with my code. I can now get my expected result. Thanks – user3390652 Apr 02 '17 at 14:04
  • I'm glad that it helped, I found bytes really confusing to begin with and wrote a whole [series of blogposts](http://sketchytech.blogspot.co.uk/search/label/bytesforbeginners) to get my head around them. They might be of use, if not to you then others reading this. – sketchyTech Apr 02 '17 at 14:12