program stops before EOF

This is a discussion on program stops before EOF within the C Programming forums, part of the General Programming Boards category; Hi I have this program set to read a binary file and output each bit as 2 bytes of data ...

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Question program stops before EOF

    Hi I have this program set to read a binary file and output each bit as 2 bytes of data to another file.
    The conversion is fine, however the program just doesn't read the entire input file it stops in the middle.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    FILE* fpread;
    FILE* fpwrite;
    char c;
    int buffer;
    int buffer2=0;//starts with low impulse
    int a;
    	
    
    int main (void)
    {
    //Open Data File
    fpread = fopen("dta.tsc", "rb");
    fpwrite = fopen("dta_READY.bin", "wb");
    	
    
    //Read File and Convert each Bit to 2 Bytes
    	
    
    c = getc(fpread);
    
    while ( c !=EOF )
    {
    	
    	for(a = 7; a >= 0; a--)
    	{
    	
    	putc (buffer2,fpwrite);
    	buffer = (int)c;
    	buffer = buffer>>a;
    	buffer = buffer &1;
    	if (buffer==1)
    		{
    		buffer = buffer2 ^ 1; 	
    		putc(buffer,fpwrite);
    		}
    	else
    		{
    		buffer = buffer2;
    		putc(buffer2,fpwrite);
    		}
    	
    	buffer2 = buffer ^ 1;//buffer2 equals buffer XOR 1
    	}
    
    	c = getc(fpread);
    	
    }
    fclose(fpread);
    fclose(fpwrite);
    return 0;
    }
    I am just trying to understand why it doesn't read my entire input file... any ideas? I would think the while(c!=EOF) would have done the job what am I missing????

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ye canna store EOF inside a char variable. You'll need to make c an int, if you want comparisons against EOF to be meaningful.

  3. #3
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    yup that fixed it! just changed
    Code:
    int c
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding an #include stops my program from compiling
    By angelscars in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2005, 05:24 PM
  2. program terminates when loop stops
    By Jherry in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2005, 09:23 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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