1

EDIT: I now realize the question i need to ask is how i will catch the carriage returns that are in the dat file "^M" throw off my output like i have shown below.

My program reads characters from a file, places them into an array, and once the array is full it dumps the input. The file contains special characters that I'm guessing might be causing the problem. I'm reading in characters and then printing their numeric values in hex format, then on the next line I want to print the same info in character form.

Can anyone tell me why my for loop seems to jump around? Is the array maybe being loading incorrectly?

file.dat FILE -- include tabs after of

This is a test of               program^M

Special characters are: ^L ^H ^K

OUTPUT: -- the output is printed with the formatting %x

54 68 69 73  69 73  61  74 65 73 74  6f 

66   70 72 6f 67 72 61 6d d  53 70 

65 63 69 61 6c  63 68 61 72 61 63 74 65 72 73 

 61 72 65 3a  c  8  b   ffffffff 72 73 

The output is correct in hex form, when translated it is the output i wanted and needed

OUTPUT: -- the output is wrong out of order

T h i s  i s  a  t e s t  o 

   S p  o g r a m 3 

e c i a l  c h a r a c t e r s 

 a r e :  

                ? r s 

This output is obviously wrong and very confusing to me. I don't understand how a simple for loop is causing this output.

CODE:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void print_group(char array[]);
void print_space(int num);
void printbits(int bits);
int main()
{
    char array[16];
    char i_file;
    int count = 0;
    FILE *fp;
    int bits = 0;
    int a = 0;

    fp = fopen("file.dat","r");

    if( fp == NULL)
    {
        printf("ERROR");
    }
    else
    {
        while (!feof(fp)) /*while pointer hasnt reached end of file continue loop*/
        {
            array[count] = fgetc(fp);

            if(count == 15 || feof(fp))
            {
                print_group(array);
                count = -1;
                printf("\n");
            }
            count++;
        }
    }

    fclose(fp);

    return 0;
}

void print_group(char array[])
{
    int a;
    int num;

    for(a = 0; a <= 15; a++)
    {
        /*This for loop wil print the numbers that are associated with the dump
        of the array.*/
        if(array[a] == ' ' || array [a] == '\t' || array[a] == '\n' || array[a] == '\?')
        {
            printf("20 ");
        }
        else
            printf("%x ",array[a]);
    }

    printf("\n");

    for(a = 0; a <= 15; a++)
    {
        /*This for loop wil print the characters that are associated with the dump
        of the array.*/
        if (array[a] == ' ' || array [a] == '\t' || array[a] == '\n' || array[a] == '\?') {
            printf(" ");
        }
        else
            printf("%c ",array[a]);
    }
}

void print_space(int num)
{}
john wayne
  • 17
  • 6
  • Try `if(array[a] <= ' ')` in the two `for` loops. – user3386109 Sep 29 '15 at 18:37
  • One hint: if your filesize is not a multiple of 16 bytes, the last part of the last chunk read from the file, will contain data from the previous read chunk. To get rid of it after you `print_group(array);` make the array 0: `memset(array, 0, 16)`. Also when printing in hex, specify `%02X ` format specifier instead of `%x `, to make it clear that it's a hex string. – CristiFati Sep 29 '15 at 18:38
  • 1
    Though it's not likely responsible for your unexpected output, you do use `feof()` incorrectly. See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong. – John Bollinger Sep 29 '15 at 18:40
  • Side note: in the first output loop, the `if` statement should be removed. All of the hex values should be printed as is. – user3386109 Sep 29 '15 at 18:45
  • What are hex number of your special characters? – Orest Hera Sep 29 '15 at 18:58
  • Another hint is that the "special" characters really might be special, and perhaps be treated as commands to the display. Consider what happens when you print `"\n"`... – Bo Persson Sep 29 '15 at 19:04

3 Answers3

1

while(!eof) is wrong

It is always necessary to check the return value of a read (either an fread(), or an fscanf(), or an fgetc()) before calling feof().

like it enters the loop one more time than you expect. If there is a read error, the loop never terminates.

Try :

int c;

while ((c = fgetc(fp)) != EOF) {
    // do something with c
}
if (ferror(fp)) {
    // handle the error, usually exit or return
} else {
    // continue execution
}

There are lot of other posts explaining this.

resultsway
  • 12,299
  • 7
  • 36
  • 43
0

I'm not sure, if this will help. But in the case where you have reached end of file and the array index has not reached 15, are you not printing the previously inserted characters of the array.

Maybe your array index should go from 0 - till the count.

This could be a reason for the junk characters that are being displayed.

Sharanya
  • 74
  • 7
  • good point, when i tried this the only problem is it can only enter the function with count of 15, or end of file… and when its at the end of the file the count is still 13 so it prints memory locations anyways. if you have any other suggestions that would be great. I can't think of a proper method to count – john wayne Sep 29 '15 at 23:34
0

^M is carriage return. When you print it, the cursor returns to the leftmost position, but doesn't move to the next line. The second batch of 16 characters is f program^M Sp, so after ^M is printed and carriage returned, Sp overwrites the previous contents (that is f pr).

user58697
  • 7,808
  • 1
  • 14
  • 28