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)");
11
  • Might this answer help you out maybe? Commented Apr 2, 2017 at 12:59
  • 1
    What is wrong to write data directly to disk? Commented Apr 2, 2017 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.. Commented Apr 2, 2017 at 13:04
  • @vadian I just want to learn something different. Commented Apr 2, 2017 at 13:07
  • 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 stackoverflow.com/questions/39075043/… Commented Apr 2, 2017 at 13:53

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

please see my comments above :)
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.
Your answer has helped me figure out what is wrong with my code. I can now get my expected result. Thanks
I'm glad that it helped, I found bytes really confusing to begin with and wrote a whole series of blogposts to get my head around them. They might be of use, if not to you then others reading this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.