I'm currently trying to create a code in Cygwin using pure C to display ASCII control characters that may be present in the text file in a ASCII control characters non-printable form.

I can call to a text file to display its contents but in normal ASCII characters but I cannot figure out how to display NON-printable characters from the text file if anyone can tell what I need to do please.

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


int main(int argc, char *argv[])
{ 
	char ch;   
	FILE *data;
	char fname[100];
	
	
	if (argv[1]!=NULL) 
	
	{
			strcpy(fname, argv[1]);
			while(access(fname, F_OK) !=0)
			{
				perror("Error");
				printf("\nspecify correct file to display\n");
				gets(fname);				
			}
	}
			
    else if (argv[1]==NULL)
	
	{
	            strcpy(fname, argv[1]);
				while(access(fname, F_OK) !=0)
				{
				   perror("Error");
				   printf("\nspecify correct file to display\n");
				   gets(fname);
				}
	}

			data = fopen (fname, "r");
			ch = fgetc(data);  
			while( ch != EOF) 
			{ 
				putchar(ch);  
				ch = getc(data);  
			}
			fclose(data);  
			return (EXIT_SUCCESS);
	}

As AD said. Since you are printing non-printable characters, you can't very well print them, so you have to print something else in it's place.

Add the value 'A' to the character if it's < SPACE and do as The Dragon suggests: putchar(ch + 'A'); Also, PLEASE format your code better. Indenting 50 spaces and inconsistently makes your code almost unreadable. See this

to try and clarify, by adding the value of 'A' (0x41) to any byte that has a value less than the space ' ' (0x20) character, and printing the resulting char appended to a caret '^', the ascii values will be printed like so

value    disp
-----    ----
0x00      ^A
0x01      ^B
0x02      ^C
...
...
0x1E      ^_
0x1F      ^`

as to posting code with indentations, you have your editor of choice treating TAB's as tab characters ('\t'). which is probably the default behavior. especially when you mix that up with spaces for indenting, you get a result that looks clean in your editor, but is wildly variable results in most other editors.

and part of the problem is with this site, because it treats all TABS as 8 spaces wide. many editors make tabs 4 or 5 spaces wide. so even if you only use TABs consistently, it still will look ugly here, because it gets so spread out, and will wrap across the screen

the trick is to change your editor settings so that all TAB characters are converted to a certain number of spaces. there should be an option for this under preferences or settings. set it anywhere from 3-8 spaces. I prefer 4. by forcing tabs to spaces (whatever amount) it will cause your code to always look consistent and clean no matter what editor opens it, or where you copy-and-paste it.

this forced conversion of TABs to spaces is actually required by most professional software dev companies, so you might as well get used to it now. :)

.

Edited 6 Years Ago by jephthah: n/a

thanks for the advice on how to make my code look better, I occasionally try it like that because it makes reading what functions work with what easier from my perspective but I will take your advice on hand.

Also I use notepad++ & I'm not too sure about the methods above I've never done that before and was hoping to get more detailed explanations about it with my code.

I'm not sure about what values between 1-32 where to add ^A and


Add the value 'A' to the character if it's < SPACE and do as The Dragon suggests:
putchar(ch + 'A');

do you mean if (A<" ") do putchar(ch +'A');

sorry guys I still have no idea

non-printable codes have the ascii values from 0 - 31

ascii codes for capital letters start at value 65. the charcter for value 65 is 'A'.

or in other words, a char 'A' has the value 65

if you have the values 0 - 31 in some value, you can't print them as they are... tehy don't print to the terminal. (except for newline tab and backspace, and maybe bell, but never mind that for now)

so print them instead as a sort of code:

^A
^B
^C
...etc...

consider this:

for (i = 0; i < 32; i++)
   printf("ASCII %02d  =  ^%c\n", i, ('A' + i));

if it doesnt make sense still, then put it in a test program and run it.

.

Edited 6 Years Ago by jephthah: n/a

tested it and it displays all 32 ascii non char but i'm not sure i'm clarifying myself the program is suppose to ask the user to input the correct text file to be read from the command line then when in putted correctly displays the contents, I want to turn that text file's contents to display non ascii characters for e.g.

text file contents: hello how are you? = non printing ascii character equivalent.

sorry if I seem to have interpreted incorrectly or I'm just slow

>>text file contents: hello how are you? = non printing ascii character equivalent

Huh? You mean do the opposite of what we have been telling you? Convert the letter 'h' into some non-printing character such as '\0'? That make absolutely no sense.

>>text file contents: hello how are you? = non printing ascii character equivalent

Huh? You mean do the opposite of what we have been telling you? Convert the letter 'h' into some non-printing character such as '\0'? That make absolutely no sense.

seriously exactly that i will post up the question here: Modify the program to display ASCII control characters that may be present in the text file in a non-printable form, for example a tab character could be represented as \t. You should investigate the possible control characters and include these in your testing.

>>seriously exactly that
No it isn't. Look at the chart here and you will find a list of all the escape characters that might be found in text files. Your job is to convert them into readable form. For example if you find the tab character which is 0x0a in the file then you would display it as "\t".

To start your program just make a list (array) of all the escape character decimal or hex values, such as 0x0a, 0x09, 0x0b, 0x08, etc. Then you will have to read the file one character at a time and check if its in the array. If it is, then replace it with the printable text as shown in the "Escape Sequence" column of that web site.

This article has been dead for over six months. Start a new discussion instead.