Solved

I/o problems

Posted on 2000-03-02
7
163 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

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.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

627 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