Ok guys, I'm back with new code. I got help from my professor on the problem, but the USB drive I'm using deleted all of his work and I can't remember what he did to get it all to work. I need to get the file (titled lab2.dat):
This is a test of lab2
Special characters are:
to print chars with spaces between and the hex representation beneath them. So far I am having a lot of trouble remembering what he did. Since my class is very new to C, I know it couldn't have been hard or complex, but since my class is new to C, I don't know what he did. Can anyone help me?

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

int findend(char *);
int charcount = 0;
const int MAX = 81;

int main(void){
    int i = 0;

    char input[MAX] = "lab2.dat"; 
    FILE *file = fopen (input, "r");
    if  ( file == NULL ) {
       perror ( "File open failure" );}
    else{ 
         while(fgets(input, sizeof input, file)!=NULL){
                 fprintf(stdout, "%s", input);//print the file out
                 for(i = 0; i < (MAX); i++)

                       if (input[i] != 0x09)
                           fprintf(stdout, "%c  ", input[i]);
                       else
                           fprintf(stdout, "\\t  ");
                 }    
                 for(i = 0; i < MAX; i++)
                       fprintf(stdout, "%00x  ", input[i]); 
    }
    system("PAUSE");
}

Recommended Answers

All 2 Replies

In line 13 : it should be unsigned char. Also if you want to print the hexadecimal equivalent, try it by using 0x%02x instead of 00x. For example see this code.

int main()
{
    unsigned char array[] = {"Hello world"};

    int i;

    for( i = 0; i <strlen(array); i++)
    {
        printf("%c",array[i]);
    }

    printf("\n\n");

    for(i = 0; i < strlen(array); i++)
    {
        printf("0x%02x ", array[i]);
    }
}

It prints the hex equivalent of each chararcter. Note that the "0x%02x" format works best if bytes are 8 bits, that's not guaranteed, but it's almost certainly the case on any system you're likely to use. You can also use 0x%x.

try it by using 0x%02x instead of %00x
You can also use 0x%x.

printf() supports an alternate formatting flag that will prefix 0x or 0X for you:

printf("%#02x ", array[i]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.