Writing raw bytes to a file

This is a discussion on Writing raw bytes to a file within the C++ Programming forums, part of the General Programming Boards category; Hi.. I'm a relative n00b to c++ programming, and I need some help I'm writing a program (specific details withheld ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Writing raw bytes to a file

    Hi.. I'm a relative n00b to c++ programming, and I need some help

    I'm writing a program (specific details withheld by me, the secretive little *&$# ) which requires me to write raw bytes to a file.

    I'm stumped as to how this might be achieved, and have searched in several places.

    HELP!! (Please!)

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Code:
    int main (void)
    {
    FILE *myfile;
    int mybyte=1;
    
    if ((myfile=fopen ("file","wt"))==NULL)
        {
        printf ("File could not be opened\n");
        return 0;
        }
    
    fputc (mybyte,myfile);
    
    fclose (myfile);
    
    return 0;
    }
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Open the file as binary and use write to copy a block of data to it:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ofstream bout ( "test.txt", std::ios::binary );
    
      char a[] = "This is a test";
      bout.write ( a, sizeof a );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Writing Raw Bitmap File
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 06-05-2005, 12:06 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21