i need your help about writing data to file

This is a discussion on i need your help about writing data to file within the C++ Programming forums, part of the General Programming Boards category; I've read the LZW compression algorithm. And i know i must use 12 bit number to code the string. But ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Question i need your help about writing data to file

    I've read the LZW compression algorithm. And i know i must use 12 bit number to code the string.
    But i dont know how to write this code 12 bit to file. Because when using fwrite function, it just accepts bytes (not bit).
    How can i do that with bit. Someone tell me do this :
    write 16bit (2 bytes) , certainly remaining 4 bit from the next code. And add the next 16 bit to file, surely contains 8 bit from the next code, and so one. I think it is right, isn it?
    But how i can do that?
    Please suggesting me all the technologies i have to learn for this purpose.
    Or give me the link to page that contain this topic or tutorial.
    Thanks

  2. #2
    Banned
    Join Date
    Aug 2004
    Posts
    128
    i dont knwo anything about LZW algorithm
    or even fi what your saying is correct,
    but all you need to do to use binary
    i believe is open the file in binary,


    Code:
    ifstream file;
    file.open("myfile.txt", ios::in | ios::binary);

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Code:
    #include <iostream>
    #include <bitset>
    
    using namespace std;
    
    int main()
    {
    	// 16 bits each
    	short num;
    	
    	// file to be written
    	ofstream file_in("file");
    
    	// 15 is equal to 000...01111
    	num = 15;
    	file_in << num;
    	file_in.close();
    	
    	// reseting, not necessary, but to make sure it works
    	num = 0;
    
    	// Lets just read 15 again
      ifstream file_out("file");
      file_out >> num;
      
    	cout << "The number is " << num << endl;
    	cin.get();
    
      return 0;
    }
    Hope that helps... also, have a look at bitset class, it might be useful.
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    but all you need to do to use binary
    i believe is open the file in binary,
    The issue is how to write bits when all you can work with are bytes.

    john_tran:
    You must arrange the bits 8 at a time, into a byte. If you want to write 10010, then 00010, you have to somehow make it into 10010000 and 10000000 instead (I put extra 0's after the 10 so that it would be 8 bits in total). Then you can write them one at a time to the file.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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