Solved

I/o problems

Posted on 2000-03-02
7
156 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
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

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

Author Comment

by:Lora
Comment Utility
Can You write this program , please
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
#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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 18

Expert Comment

by:deighton
Comment Utility
deighton changed the proposed answer to a comment
0
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
Comment Utility
#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
Comment Utility
Can You please tell me what this means
c^=c
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now