Write to binary file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zach

    #1

    Write to binary file

    Can someone help me out, I can't figure out what I'm doing wrong to
    write to a file in binary mode.

    What's wrong with my code?

    <?php

    $fileName = "something.dat" ;
    $string = "This is a string of text";

    $ptr = fopen($fileName , 'wb');

    fwrite($ptr, $string);
    fclose($ptr);

    ?>
  • Rik Wasmus

    #2
    Re: Write to binary file

    On Sat, 08 Dec 2007 21:47:13 +0100, zach <wackzingo@gmai l.comwrote:
    Can someone help me out, I can't figure out what I'm doing wrong to
    write to a file in binary mode.
    >
    What's wrong with my code?
    >
    <?php
    >
    $fileName = "something.dat" ;
    $string = "This is a string of text";
    >
    $ptr = fopen($fileName , 'wb');
    >
    fwrite($ptr, $string);
    fclose($ptr);
    >
    ?>
    Nothing wrong with the code, what's wrong with the result?
    --
    Rik Wasmus

    Comment

    • Paul Lautman

      #3
      Re: Write to binary file

      zach wrote:
      Can someone help me out, I can't figure out what I'm doing wrong to
      write to a file in binary mode.
      >
      What's wrong with my code?
      >
      <?php
      >
      $fileName = "something.dat" ;
      $string = "This is a string of text";
      >
      $ptr = fopen($fileName , 'wb');
      >
      fwrite($ptr, $string);
      fclose($ptr);
      Is that a game like spot the difference?

      Thing I notice straight away is the lack of closing php tag.


      Comment

      • zach

        #4
        Re: Write to binary file

        Rik Wasmus wrote:
        On Sat, 08 Dec 2007 21:47:13 +0100, zach <wackzingo@gmai l.comwrote:
        >
        >Can someone help me out, I can't figure out what I'm doing wrong to
        >write to a file in binary mode.
        >>
        >What's wrong with my code?
        >>
        ><?php
        >>
        >$fileName = "something.dat" ;
        >$string = "This is a string of text";
        >>
        >$ptr = fopen($fileName , 'wb');
        >>
        >fwrite($ptr, $string);
        >fclose($ptr) ;
        >>
        >?>
        >
        Nothing wrong with the code, what's wrong with the result?
        Maybe nothing, I just thought that by writing text in binary mode it
        would make it unreadable (for the most part). No matter what I write or
        how much its still just plain ascii text. When I write text in C it
        pretty much unreadable.

        Comment

        • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

          #5
          Re: Write to binary file

          zach wrote:
          $ptr = fopen($fileName , 'wb');
          Windows or Linux host?

          --
          ----------------------------------
          Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

          Me cansé, no escribo más Tag's ... 8-(

          Comment

          • zach

            #6
            Re: Write to binary file

            Paul Lautman wrote:
            zach wrote:
            >Can someone help me out, I can't figure out what I'm doing wrong to
            >write to a file in binary mode.
            >>
            >What's wrong with my code?
            >>
            ><?php
            >>
            >$fileName = "something.dat" ;
            >$string = "This is a string of text";
            >>
            >$ptr = fopen($fileName , 'wb');
            >>
            >fwrite($ptr, $string);
            >fclose($ptr) ;
            >
            Is that a game like spot the difference?
            >
            Thing I notice straight away is the lack of closing php tag.
            >
            >
            That was a copy paste error I have it in my script.

            Comment

            • zach

              #7
              Re: Write to binary file

              Iván Sánchez Ortega wrote:
              zach wrote:
              >
              >$ptr = fopen($fileName , 'wb');
              >
              Windows or Linux host?
              >
              Windows, as I replied to Rik, it might be working correctly and I'm just
              expecting something different. The result is it creates the file, and
              writes plain ascii text and I thought it would be unreadable.

              Comment

              • Rik Wasmus

                #8
                Re: Write to binary file

                On Sat, 08 Dec 2007 22:06:18 +0100, Paul Lautman
                <paul.lautman@b tinternet.comwr ote:
                zach wrote:
                >Can someone help me out, I can't figure out what I'm doing wrong to
                >write to a file in binary mode.
                >>
                >What's wrong with my code?
                >>
                ><?php
                >>
                >$fileName = "something.dat" ;
                >$string = "This is a string of text";
                >>
                >$ptr = fopen($fileName , 'wb');
                >>
                >fwrite($ptr, $string);
                >fclose($ptr) ;
                >
                Is that a game like spot the difference?
                >
                Thing I notice straight away is the lack of closing php tag.
                Newsreader error, the ?is actually there.
                --
                Rik Wasmus

                Comment

                • Rik Wasmus

                  #9
                  Re: Write to binary file

                  On Sat, 08 Dec 2007 22:07:02 +0100, zach <wackzingo@gmai l.comwrote:
                  Rik Wasmus wrote:
                  >On Sat, 08 Dec 2007 21:47:13 +0100, zach <wackzingo@gmai l.comwrote:
                  >>
                  >>Can someone help me out, I can't figure out what I'm doing wrong to
                  >>write to a file in binary mode.
                  >>>
                  >>What's wrong with my code?
                  >>>
                  >><?php
                  >>>
                  >>$fileName = "something.dat" ;
                  >>$string = "This is a string of text";
                  >>>
                  >>$ptr = fopen($fileName , 'wb');
                  >>>
                  >>fwrite($ptr , $string);
                  >>fclose($ptr );
                  >>>
                  >>?>
                  > Nothing wrong with the code, what's wrong with the result?
                  >
                  Maybe nothing, I just thought that by writing text in binary mode it
                  would make it unreadable (for the most part).
                  You misunderstand binary I think. It just means no newline 'translation'
                  takes place.
                  No matter what I write or how much its still just plain ascii text. When
                  I write text in C it pretty much unreadable.
                  The string is here is ascii text (well, it might have characters outside
                  ascii depending on the string the particular character encoding used). So
                  in the file will be what you told $string is. Checking a hex editor, the
                  contents of your file should prove to be exactly the same bytes as the
                  string in your script. In C data types are handled quite more complex
                  (well, under water this is also true for PHP, but you needn't concern
                  yourself with it while writing in PHP), possibly generating a different
                  representation when outputting the raw data.

                  What is the problem/issue you are actually facing that you want this
                  'binary' writing?
                  --
                  Rik Wasmus

                  Comment

                  • zach

                    #10
                    Re: Write to binary file

                    Rik Wasmus wrote:
                    On Sat, 08 Dec 2007 22:07:02 +0100, zach <wackzingo@gmai l.comwrote:
                    >
                    >Rik Wasmus wrote:
                    >>On Sat, 08 Dec 2007 21:47:13 +0100, zach <wackzingo@gmai l.comwrote:
                    >>>
                    >>>Can someone help me out, I can't figure out what I'm doing wrong to
                    >>>write to a file in binary mode.
                    >>>>
                    >>>What's wrong with my code?
                    >>>>
                    >>><?php
                    >>>>
                    >>>$fileName = "something.dat" ;
                    >>>$string = "This is a string of text";
                    >>>>
                    >>>$ptr = fopen($fileName , 'wb');
                    >>>>
                    >>>fwrite($pt r, $string);
                    >>>fclose($ptr) ;
                    >>>>
                    >>>?>
                    >> Nothing wrong with the code, what's wrong with the result?
                    >>
                    >Maybe nothing, I just thought that by writing text in binary mode it
                    >would make it unreadable (for the most part).
                    >
                    You misunderstand binary I think. It just means no newline 'translation'
                    takes place.
                    >
                    >No matter what I write or how much its still just plain ascii text.
                    >When I write text in C it pretty much unreadable.
                    >
                    The string is here is ascii text (well, it might have characters outside
                    ascii depending on the string the particular character encoding used).
                    So in the file will be what you told $string is. Checking a hex editor,
                    the contents of your file should prove to be exactly the same bytes as
                    the string in your script. In C data types are handled quite more
                    complex (well, under water this is also true for PHP, but you needn't
                    concern yourself with it while writing in PHP), possibly generating a
                    different representation when outputting the raw data.
                    >
                    What is the problem/issue you are actually facing that you want this
                    'binary' writing?
                    Well its nice to know I was writing my code right, lol. I was expecting
                    it to look different, like binary writing.

                    Comment

                    • Paul Lautman

                      #11
                      Re: Write to binary file

                      Rik Wasmus wrote:
                      On Sat, 08 Dec 2007 22:06:18 +0100, Paul Lautman
                      <paul.lautman@b tinternet.comwr ote:
                      >
                      >zach wrote:
                      >>Can someone help me out, I can't figure out what I'm doing wrong to
                      >>write to a file in binary mode.
                      >>>
                      >>What's wrong with my code?
                      >>>
                      >><?php
                      >>>
                      >>$fileName = "something.dat" ;
                      >>$string = "This is a string of text";
                      >>>
                      >>$ptr = fopen($fileName , 'wb');
                      >>>
                      >>fwrite($ptr , $string);
                      >>fclose($ptr );
                      >>
                      >Is that a game like spot the difference?
                      >>
                      >Thing I notice straight away is the lack of closing php tag.
                      >
                      Newsreader error, the ?is actually there.
                      OE Quotefix is doing it. But when someone posts such an open question, it's
                      like a game trying to spot anything that could possibly be wrong.


                      Comment

                      • Paul Lautman

                        #12
                        Re: Write to binary file

                        zach wrote:
                        Rik Wasmus wrote:
                        >On Sat, 08 Dec 2007 22:07:02 +0100, zach <wackzingo@gmai l.comwrote:
                        >>
                        >>Rik Wasmus wrote:
                        Well its nice to know I was writing my code right, lol. I was
                        expecting it to look different, like binary writing.
                        Maybe next time you have a question like this, you could actually tell us
                        the problem that you think you are facing, rather than have everone try to
                        guess?


                        Comment

                        • zach

                          #13
                          Re: Write to binary file

                          Paul Lautman wrote:
                          zach wrote:
                          >Rik Wasmus wrote:
                          >>On Sat, 08 Dec 2007 22:07:02 +0100, zach <wackzingo@gmai l.comwrote:
                          >>>
                          >>>Rik Wasmus wrote:
                          >Well its nice to know I was writing my code right, lol. I was
                          >expecting it to look different, like binary writing.
                          Maybe next time you have a question like this, you could actually tell us
                          the problem that you think you are facing, rather than have everone try to
                          guess?
                          >
                          >
                          Thanks, I'll try to be more specific in the future. But I'll also
                          suggest you look at Rik's response and learn from him.

                          Comment

                          • Paul Lautman

                            #14
                            Re: Write to binary file

                            zach wrote:
                            Paul Lautman wrote:
                            >zach wrote:
                            >>Rik Wasmus wrote:
                            >>>On Sat, 08 Dec 2007 22:07:02 +0100, zach <wackzingo@gmai l.com>
                            >>>wrote:
                            >>>>Rik Wasmus wrote:
                            >>Well its nice to know I was writing my code right, lol. I was
                            >>expecting it to look different, like binary writing.
                            >Maybe next time you have a question like this, you could actually
                            >tell us the problem that you think you are facing, rather than have
                            >everone try to guess?
                            >>
                            >>
                            Thanks, I'll try to be more specific in the future. But I'll also
                            suggest you look at Rik's response and learn from him.
                            Which of Rik's responses and what is it I'm supposed to be learning?


                            Comment

                            Working...