473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

creating a testfile for catv

What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:

#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");
else
while (--argc 0)
if ((fp = fopen(*++argv, "r")) == NULL)
{
printf("catv can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}

return 0;
}

/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int ch;
int result;

while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch)) {
/* ch is a control character */
int result;

/*
* The two characters we want to print. The first is '^';
* we don't know yet what the second is.
*/
int ch1 = '^';
int ch2;

/* Try to print the first character. */
result = putchar(ch1);
if (result == EOF) {
/* Failed, terminate the loop */
break;
}

if (ch == '\177') {
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else {
/*
* ch is another control character.
* Transform 1 to 'A', 2 to 'B', etc. using
* our intimate knowledge of ASCII encoding.
*/
ch2 = ch | 0100;
}

/* Print as above */
result = putchar(ch2);
if (result == EOF) {
break;
}
}

else putc(ch, ofp);

}

}
// gcc -o catv catv4.c
// catv text24.txt >text43.txt

Abridged output from text43 is:
^JTarget: i386-pc-mingw32^J

My guess is that J is what you get when you add octal 0100 to a carriage
return. Am I correct to think that the first 32 chars are control chars?
DEL appears to be 117 as well.

How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)
--
C. Gordon Liddy

"Virile, vigorous, potent"
Mar 30 '08 #1
9 1257
c gordon liddy wrote:

<snip>
How would I populate a file containing non-ascii and ctrl chars for
test purposes? (Does anyone have one?)
The easiest way is to open the file in a hex editor and fill in the
character codes directly.

Mar 30 '08 #2
On Sun, 30 Mar 2008 02:22:25 -0700, c gordon liddy wrote:
What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:
[snippage]
How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)
mkchars.c:

#include "stdio.h"

unsigned char i;

int main(int argc, char * argv[])
{
unsigned char buffer[256];
do { buffer[i] = i; } while(++i);
fwrite(buffer, 256, 1, stdout);

return 0;
}

make mkchars
../mkchars >samplechars

../catv samplechars
Many terminal emulators will react oddly if you fail to redirect. Don't
../mkchars

Many editors will behave badly if you try to edit samplechars.

Martin
--
Martin Golding DoD #0236 | fo*****@comcast.net
Always code as if the person who ends up maintaining your code will be a
violent psychopath who knows where you live.
y
Mar 30 '08 #3
Martin Golding wrote:
On Sun, 30 Mar 2008 02:22:25 -0700, c gordon liddy wrote:
>What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:
[snippage]
>How would I populate a file containing non-ascii and ctrl chars for
test purposes? (Does anyone have one?)

mkchars.c:

#include "stdio.h"
ITYM <stdio.h>
unsigned char i;

int main(int argc, char * argv[])
{
unsigned char buffer[256];
do { buffer[i] = i; } while(++i);
fwrite(buffer, 256, 1, stdout);
If the OP meant "ASCII control characters", then perhaps you should stop
at 31?

As far as "non-ascii" characters, I don't quite get what the OP means.
Does he mean characters from other encodings, like extended ASCII,
EBCDIC etc.?
return 0;
}

make mkchars
./mkchars >samplechars

./catv samplechars
Many terminal emulators will react oddly if you fail to redirect.
Don't ./mkchars

Many editors will behave badly if you try to edit samplechars.

Martin
Mar 30 '08 #4

"Martin Golding" <fo*****@comcast.netwrote in message
news:pa*********************@comcast.net...
On Sun, 30 Mar 2008 02:22:25 -0700, c gordon liddy wrote:
>What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:
[snippage]
>How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)

mkchars.c:

#include "stdio.h"

unsigned char i;

int main(int argc, char * argv[])
{
unsigned char buffer[256];
do { buffer[i] = i; } while(++i);
fwrite(buffer, 256, 1, stdout);

return 0;
}

make mkchars
./mkchars >samplechars

./catv samplechars
Many terminal emulators will react oddly if you fail to redirect. Don't
./mkchars

Many editors will behave badly if you try to edit samplechars.
Thanks Martin. You got me rolling with this, so I've got output to tune
things up with.

After I got a look at the output, I found a link that explained control and
non-ascii chars:
http://www.robelle.com/smugbook/ascii.html
It looks like ascii chars are 0 through 127 decimal. Control chars include
the first 32 and DEL at octal 177 (7 +8*7+64*1=7+56+64=127 decimal, the
ultimate ascii char). I'm confused as to the taxonomy of chars between
decimal 32 and decimal 37.

My output with Martin's output catv'ed is:
^@^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X ^Y
, which is good, in that I seem to have the behavior for control chars
correct. Clearly, other chars aren't making it through yet, so I need some
more time at the chalkboard for that.

I'll finish with Martin's output. All bets are off on how they are
displayed on a usenet message:




!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~?,f".??^?S<OZ ''"".--~Ts>ozY
*
--

c gordon liddy
Mar 30 '08 #5


"Keith Thompson" <ks***@mib.orgwrote in message
news:87************@kvetch.smov.org...
Here's how I'd probably write it:
#include <stdio.h>
int main(void)
{
int c;
for (c = 0; c <= 255; c ++) {
putchar(c);
}
return 0;
I modified this program and now have a good test file:

#include <stdio.h>
int main(void)
{
int c;
printf ("the quick brown fox\n");
for (c = 0; c <= 40; c ++) {
putchar(c);
}
printf ("jumps over\n");
for (c = 100; c <= 200; c ++) {
putchar(c);
}
printf ("the lazy dog\n");
return 0;
}
// gcc -o mkchars mkchars2.c
// mkchars >text42.txt

Current output with catv is:
the quick brown fox^J^@^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^ V^W^X^Y

So after it reads and writes the control characters, it doesn't want to
print anything more. Here's the current version of catv. I cleaned up the
bracing, thinking that that would be where I was in trouble, but that's not
it. I also added some functionality for non-ascii chars. Again no luck.

#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");
else
while (--argc 0)
if ((fp = fopen(*++argv, "r")) == NULL)
{
printf("catv can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}

return 0;
}

/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int ch;
int result;
int ch1 = '^';
int ch2;
int ch3 = 'M';

while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch))
{

result = putchar(ch1);
if (result == EOF)
{
/* Failed, terminate the loop */
break;
}

if (ch == '\177')
{
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else
{
/*
* ch is another control character.
* Transform 1 to 'A', 2 to 'B', etc. using
* our intimate knowledge of ASCII encoding.
*/
ch2 = ch | 0100;
}

/* Print as above */
result = putchar(ch2);
if (result == EOF)
{
break;
}
// outer brace of if (iscntrl(ch))
}
else if (!isascii(ch))
{
result = putchar(ch3);
if (result == EOF)
{
break;
}

}
else putchar(ch);
// outer brace of while control
}
// outer brace of function
}
// gcc -o catv catv7.c >text22.txt 2>text23.txt
// catv text42.txt >text43.txt

I'm new to gcc. Does it have a debugger?

I get no warnings now, and I think I should at least with int c declared and
never referenced. How do I turn up the warnings?

--
c gordon liddy
Mar 30 '08 #6


"santosh" <sa*********@gmail.comwrote in message
news:fs**********@registered.motzarella.org...
>c gordon liddy wrote:

<snip>
>How would I populate a file containing non-ascii and ctrl chars for
test purposes? (Does anyone have one?)

The easiest way is to open the file in a hex editor and fill in the
character codes directly.
Where does a person find such a thing?
--
Mar 30 '08 #7
C. Gordon Liddy said:

<snip>
Current output with catv is:
the quick brown fox^J^@^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^ V^W^X^Y

So after it reads and writes the control characters, it doesn't want to
print anything more.
It is very suggestive that your output stops at Ctrl-Z, which is used as an
end-of-text-file marker by some systems. The fix is obvious - open the
file in binary mode:

<snip>
#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");
else
while (--argc 0)
if ((fp = fopen(*++argv, "r")) == NULL)
Change "r" to "rb" and re-test.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 30 '08 #8


"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:0I******************************@bt.com...
C. Gordon Liddy said:

<snip>
>Current output with catv is:
the quick brown fox^J^@^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^ V^W^X^Y

So after it reads and writes the control characters, it doesn't want to
print anything more.

It is very suggestive that your output stops at Ctrl-Z, which is used as
an
end-of-text-file marker by some systems. The fix is obvious - open the
file in binary mode:

<snip>
>#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");
else
while (--argc 0)
if ((fp = fopen(*++argv, "r")) == NULL)

Change "r" to "rb" and re-test.
Thanks, Richard. My output is coming around.

the quick brown
fox^M^J^@^A^B^C^D^E^F^G^H^I^M^J^K^L^M^N^O^P^Q^R^S^ T^U^V^W^X^Y^#^[^\^]^^^_
!"#$%&'(jumps
over^M^Jdefghijklmnopqrstuvwxyz{|}~^?MMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMthe
lazy dog^M^J

Sometimes hitting the send button on a post is the exact stimulus one needs
to realize what's in his blind spot: ctrl-z is eof for windows. rb instead
of r works. I tried to do the same thing for that the original did for del:
if (ch == '\177')
{
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else if(ch == 26)
{
/* we don't want ctrl-z coming out of here */
ch2 = '#';
}
I'm puzzled that this didn't work.

I've got some behavior for non-ascii characters. Again, I'll try to emulate
what came off the bsd site:
if (!isascii(ch)) {
if (putchar('M') == EOF || putchar('-') == EOF)
break;
ch = toascii(ch);
}

Heck, I can just paste it in wholesale. Woo-hoo.

--

Mar 31 '08 #9
On Sun, 30 Mar 2008 02:22:25 -0700, "c gordon liddy" <c@invalid.liddy>
wrote:
>What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:
snip code copied from earlier message thread

Why did you change the subject of the message and post the code again?
See the comments in the putchar thread.
>// gcc -o catv catv4.c
// catv text24.txt >text43.txt

Abridged output from text43 is:
^JTarget: i386-pc-mingw32^J

My guess is that J is what you get when you add octal 0100 to a carriage
return. Am I correct to think that the first 32 chars are control chars?
On an ASCII system, \n (as your program sees it, not as it is stored
in the file) is 0xoa which has a decimal representation of 10.
>DEL appears to be 117 as well.
You meant \177?
>
How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)
Open a file for output and try
int i;
for (i = 0; i <= UCHAR_MAX; i++)
fputc(i,fp);
then close the file.
Remove del for email
Mar 31 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: draghuram | last post by:
Hi, Is there any special support for sparse file handling in python? My initial search didn't bring up much (not a thorough search). I wrote the following pice of code: options.size =...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
3
by: ernst | last post by:
Hello, I cannot get ASP.NET running with a simple testfile called "test.aspx". It contains the following lines: <html> <head> <script language="vb" runat="server"> sub page_load()...
0
by: reidarT | last post by:
Ugyldig p toppnivet av dokumentet. Feil under behandling av ressursen file:///C:/VB/WebBrowser/WebBrowserControl/TESTFILE... "<?xml version=""1.0""?>","<XMLTest>" ^FileOpen(1,...
1
by: ReidarT | last post by:
The former post was wrong, the lines where concatenated so here it goes again. I am wrriting to a file (XML-format) and using FileOpen(1, "C:\vb\WebBrowser\WebBrowserControl\TESTFILE.XML",...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
2
by: SAL | last post by:
I would like to create a VB.net function, that builds a dynamic hyperlink using System.Web.UI.WebControls.HyperLink, but I can not find any examples on how to generate a dynamic hyperlink. Has...
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
0
by: Peter Donis | last post by:
When running a doctest text file with doctest.testfile, I noticed that universal newline support did not appear to work when module_relative is False. My text file was saved on a Windows machine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.