Solved

I/o problems

Posted on 2000-03-02
7
162 Views
Last Modified: 2010-04-15
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
Comment
Question by:Lora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 2576392
If you think that the input may contain non-graphic characters, just hexdump the file.
0
 

Author Comment

by:Lora
ID: 2576529
Can You write this program , please
0
 
LVL 18

Expert Comment

by:deighton
ID: 2576876
#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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:deighton
ID: 2576912
deighton changed the proposed answer to a comment
0
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 2576915
#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
 

Author Comment

by:Lora
ID: 2576959
Can You please tell me what this means
c^=c
0
 
LVL 18

Expert Comment

by:deighton
ID: 2579588
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question