LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2005, 07:09 AM   #1
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Rep: Reputation: 0
Question read & write binary file error in redhat


In my C program, I write the file bit by bit(for example :1010011111010000),but when I read them out bit by bit, the result is not which I have written(it become 1111010000101001). And the results are all right before it when reading.
What is wrong?????

my system is redhat 9.0
I use the fopen , fread, fwrite as so on,just make them bit by bit.

Thank you, sincerely
 
Old 06-10-2005, 09:54 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
It sounds like Endianness.
 
Old 06-10-2005, 10:25 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Are those exact value examples? My first thought was endianness too, but if you gave exact examples, that doesn't quite appear to be the case. (Although, it could still be partly to blame.)

How exactly are you writing "bit by bit?" The smallest unit you can read/write with fread/fwrite is a byte. Maybe some code examples are in order.
 
Old 06-10-2005, 08:45 PM   #4
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Question

I do not know if it is a endianness, and my example is exactly my program's result.

I pack the binary as follows.

int file_pack:ack(int size, long int value)
{
int i;
int tempint;
for (i=0; i<size; ++i, ++ptr, value = value>>1, sum = sum<<1) {
if (value & 1) sum |= 1;

if (ptr == 8) {
tempbuffer[bufferindex] = (unsigned char)sum;
bufferindex ++;
if(bufferindex == 1024){
if(fwrite(tempbuffer, 1, bufferindex, poutput) < bufferindex)
fatal_pack_unpack("\nPack error.");
/* printf("\nPack a block of data %d." , bufferindex);*/
bufferindex = 0;
}
/* printf(" %x ", tempch);*/
++num_of_packed_bytes;
++temp_num_of_packed_bytes;
sum=0;
ptr=0;
}
}
return i;
}

class file_pack
{
public:
int ptr ;/* how many bits are packed in sum so far */
int sum ; /* packed bits */
unsigned char tempbuffer[1024];
unsigned char tempbuffer[1024];
unsigned int bufferindex;

........
}



class file_unpack
{
public:
int ptr ;/* how many bits are packed in sum so far */
int sum ; /* unpacked bits */
unsigned char tempbuffer[1024];
int countbuffer;
int buffersize;
....
}

file_unpack::file_unpack(FILE *fptr)
{
ptr = 1;
sum = 0;
pinput = fptr;

num_of_unpacked_bytes = 0;
countbuffer = 0;
/*initialize the unpack process*/

if((buffersize = fread(tempbuffer, 1,1024, pinput) )< 1)
fatal_pack_unpack("\nUnpack error.");
/*printf(" %x ", tempch);*/
printf("\nUnpack 2 buffer %d char", buffersize);
sum = (int) tempbuffer[countbuffer];
countbuffer ++;
sum <<= 1;
num_of_unpacked_bytes ++;
}

long file_unpack::unpack(int size)
{
int i;

unsigned long int value = 0;
/* size == -2 means we initialize things */
if (size == -2) {
return((long)0);
}

/* size == -1 means we want to peek at the next bit without */
/* advancing the pointer */
if (size == -1)
return((long)((sum&256)>>8));
for (i=0; i<size; ++i, ++ptr, sum <<= 1) {
if (sum & 256) value |=(unsigned long) (1<<i);

if (ptr == 8) {
if(countbuffer == 1024 || countbuffer == buffersize){
if((buffersize = fread(tempbuffer, 1,1024, pinput) ) < 1)
if(feof(pinput)){
printf("\nRead out all the files.");
exit(1);
}
else
fatal_pack_unpack("\nRead input file error.");
countbuffer = 0;
/* printf("\nUnpack 2 buffer %d char", buffersize);*/
}
if(countbuffer < buffersize)
sum = (int) tempbuffer[countbuffer];
countbuffer ++;
num_of_unpacked_bytes ++;

/*printf(" %x ", tempch);*/

ptr=0;
}
}

return((long)value);
}


Before my error, I have pack some other data longer than 8 bits, they are not wrong, just from this one, error happens.
Why?
Maybe my fault, but I can not find out it.
Thanks

I have forgot that I execute the pack & unpack on the same computer(Dell optiplex).

Last edited by xcmore; 06-10-2005 at 08:48 PM.
 
Old 06-10-2005, 09:18 PM   #5
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Have you open file with "ios::binary" flag?
 
Old 06-10-2005, 09:39 PM   #6
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Original Poster
Rep: Reputation: 0
I open the file by fopen which uses "rb+", and write the file which is "wb+"
 
Old 06-10-2005, 11:20 PM   #7
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
I saw no benifit from your pack && unpack. Why dont you fwrite(value) to file and then fread from file later?
 
Old 06-11-2005, 01:33 AM   #8
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Original Poster
Rep: Reputation: 0
because i have to record non-8-bit data to file,so may be i can not align to byte.
 
Old 06-11-2005, 12:18 PM   #9
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
I don't know if it's the cause of your problem, but you'd be well advised to zero your sum and ptr variables at the start of the file_pack method, not at the end - they may contain random values first time through.
 
Old 06-11-2005, 08:20 PM   #10
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Question

I have set them zero in the construct method. Thanks.
 
Old 06-12-2005, 01:49 AM   #11
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by xcmore
I do not know if it is a endianness, and my example is exactly my program's result.
Code:
.......................
long file_unpack::unpack(int size)
{
    int i;

    unsigned long int value = 0;
    /* size == -2 means we initialize things */
    if (size == -2) {
        return((long)0);
    }

    /* size == -1 means we want to peek at the next bit without */
    /* advancing the pointer */
    if (size == -1)
        return((long)((sum&256)>>8));
    
    for (i=0; i<size; ++i, ++ptr,  value <<= 1) {      
         if (ptr == 8) {
         	if(countbuffer == 1024 || countbuffer == buffersize){
         		if((buffersize = fread(tempbuffer, 1,1024, pinput) ) < 1)
         			if(feof(pinput)){
				printf("\nRead out all the files.");
				exit(1);
            			}
         			else
         				fatal_pack_unpack("\nRead input file error.");
         		countbuffer = 0;
/*         		printf("\nUnpack 2 buffer %d char", buffersize);*/
         		}
            	if(countbuffer < buffersize)
            		sum = (int) tempbuffer[countbuffer];
            	countbuffer ++;
		num_of_unpacked_bytes ++;
		
/*printf(" %x ", tempch);*/
            	
            ptr=0;
        }     
        if ((sum >> (7-ptr)) & 1) value |= 1;  
     }

    return((long)value);
}
Before my error, I have pack some other data longer than 8 bits, they are not wrong, just from this one, error happens.
Why?
Maybe my fault, but I can not find out it.
Thanks

I have forgot that I execute the pack & unpack on the same computer(Dell optiplex).
Just a thought,
GH
 
Old 06-17-2005, 07:48 AM   #12
xcmore
LQ Newbie
 
Registered: Jun 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you for all your help!

But finally I give up ,just pack the bytes by bytes, and I have solved the problem .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C, howto read binary file into buffer Scrag Programming 24 11-27-2014 05:32 PM
Read Write for NTFS file system geeedeee Linux - Laptop and Netbook 8 06-05-2006 07:41 PM
read & write USB pen drive?? wrangler Linux - Newbie 5 03-08-2005 03:11 PM
RedHat read error on Acrobat PDF file irrlicht Linux - Software 1 11-26-2003 07:13 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration