Link to home
Start Free TrialLog in
Avatar of Lora
Lora

asked on

I/o problems

Help me to write a program that will print arbitary input in a sensible way.
As a minimum it should print non-graphic characters in octal or hexadecimal according to local custom and break long text lines.
Avatar of marcjb
marcjb
Flag of United States of America image

If you think that the input may contain non-graphic characters, just hexdump the file.
Avatar of Lora
Lora

ASKER

Can You write this program , please
#include <stdio.h>
#include<ctype.h>
#include<string.h>



main()
{
      FILE *f1,*f2;
      char file[81],x;
      char buffer[81];
      int c;

      printf("\nEnter the file Name ");
      gets(file);

      if ((f1 = fopen(file,"r"))==NULL)
      {
            puts("error");
            exit(1);
      }

      c^=c;
      while(!feof(f1))
      {
            x=fgetc(f1);
            if (x=='\n')
            {
                  buffer[c++] = x;
                  buffer[c] = NULL;
                  printf("%s",buffer);
                  c^=c;
            }
            else
            {
                  buffer[c++] = isprint(x) ? x: '.';
                  if (c == 79)
                  {
                        buffer[c] = NULL;
                        puts(buffer);
                        c^=c;
                  }

            }

      }
      fclose(f1);
      getch();
}
deighton changed the proposed answer to a comment
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lora

ASKER

Can You please tell me what this means
c^=c
Yes indeed!

c ^= c is shorthand for

c = c ^ c

^ is the 'exclusive or operator' so in bitwise terms 0 0 and 1 1 become  0

it has the same effect as c = 0; when used the way I used it. (XOR of a number with itself has to give zero)

To be honest it might be bad practise, perhaps you'd be better off using c  = 0;