What Salem is saying is that there are a couple logical error in you code.
The first is that you are only processing one line. The second is that you are printing garbage after the line is done being processed.
Let's break down the code in question:
Initialize count to zero.
Initialize BUFFER to 255 (this should probably be a #define at the top of the file since you don't need or want BUFFER to be a variable).
Code:
char line[BUFFER]; /* char array for pointers to each character in INSTREAM*/
Declare line as a character array of length BUFFER (255).
Code:
if (INSTREAM != NULL)
{
Check INSTREAM. I think this should be OUTSTREAM since you already checked instream higher up in your code.
Code:
while (fgets (line, sizeof line, INSTREAM) != NULL) /* grabs pointers for line[] */
{
Get up to sizeof(line) - 1 characters from INSTREAM and store them in line. I recommend you read up a little on exactly what will be in line if you have a line in the file that is right around limit you passed to fgets. I like the following site for quick, basic info on the standard C libraries: The C Library Reference Guide.
Code:
while(count <= sizeof line)
{
For each element of line (regardless of whether you read 2 characters or 254), process the character and print some output.
Code:
...
count++; /* adds 1 to count and continues the while loop */
}
}
}
Increment count and terminate loops/if-blocks.
The problems you're having both lie around this line:
Code:
while(count <= sizeof line)
The first time you hit this loop, count is 0, and gets incremented until the loop terminates, meaning that count is 256. You finish processing the line and read another line. For all lines other than the first, when you hit this loop, count is already 256 (you reached the end of the first line and never reinitialized count for the new line), and thus this whole loop gets skipped. You need a count = 0 between the while(fgets... loop and the while(count... loop, as Salem suggested
Code:
while (fgets (line, sizeof line, INSTREAM) != NULL) /* grabs pointers for line[] */
{
count = 0;
while(count <= ...
That while(count... loop is also the problem with your garbage variables.
Remember that line is a array of 255 characters, thus sizeof(line) is equivalent to sizeof(char) * 255, or 255 bytes, regardless of how much you read in from a given file. As a matter of fact, line would still be 255 bytes if you never read a single thing in. the sizeof operator only tells you how much memory that variable requires, not how much significant info is stored in it. Read up on sizeof.
The fgets function doesn't necessarily read in 255 characters (even though you passed in sizeof(line) for the second parameter), it's may stop when it gets an EOL or EOF. Note that line will always be null terminated whether it reads 2 characters or 254. You only care about what it read from the file, not every character in the array. The null character fgets put in line is to tell you where the significant info stops. Everything after it is garbage.
Thus, to avoid all the garbage hex values you see, change your code to something like:
Code:
while(count <= strlen(line))
That fix should get rid of all the garbage, but I let you take a crack at getting rid of the newline character you're seeing in your output.