Search code examples
carraysspecial-charactersfgetc

C for loop not printing correctly, characters out of order


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)
{}

Solution

  • ^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).