Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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.
0
Lora
Asked:
Lora
  • 4
  • 2
1 Solution
 
marcjbCommented:
If you think that the input may contain non-graphic characters, just hexdump the file.
0
 
LoraAuthor Commented:
Can You write this program , please
0
 
deightonCommented:
#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();
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
deightonCommented:
deighton changed the proposed answer to a comment
0
 
deightonCommented:
#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,"rb"))==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;
            }

            }

      }
      printf("\nAll gone");
      fclose(f1);
      getch();
}
0
 
LoraAuthor Commented:
Can You please tell me what this means
c^=c
0
 
deightonCommented:
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;

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now