Re: stream io in c
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Thu, 31 Jul 2008 02:30:50 +0100
Ron Ford <ron@xxxxxxxxxxxxxxx> writes:
On Tue, 29 Jul 2008 19:49:54 -0600, Ron Ford posted:
I think the solution in c could be as easy as instantiating a loop from
zero to 255, putchar'ing in the body of the loop, then redirecting output
to a file. If the creation of the file comes from the source, I suspect it
would have to be in 'rb' mode.
I've tried some variations with this.
int i;
for (i = 0; i <= UCHAR_MAX; i++) putchar (i);
When stdout is redirected to a file, I get a file of size 257 bytes. I
wanted to see if that would also be the case if I created the file from
source, but I've got type mismatches here:
#include <stdio.h>
int main(void)
{
FILE *fp;
char name[]="text58.txt";
fp=&name;
No need to set fp here. Just use name in the fopen call. This
assignment does no make sense anyway, since the types don't match.
int c;
if ((fp = fopen(fp, "rb")) == NULL)
Use fopen(name, "wb") here. You are opening for reading but plan to
write to this stream.
{
printf("can't open %s\n", fp);
printf("can't open %s\n", name);
return 1;
}
else
{
for (c = 0; c <= 255; c ++) {
putc(c, fp);
}
fclose(fp);
}
return 0;
}
// gcc -o chars mkchars1.c
This compiles but gcc warns of incompatible pointer types. As of now, it
tells me it can't open.
This is because you try to open it for reading and, presumably, the
file does not yet exist.
I can't find an example in K&R where they hard-code
a filename like this, so I'm a little stuck.:-(
--
Ben.
.
- References:
- stream io in c
- From: Ron Ford
- Re: stream io in c
- From: Ron Ford
- stream io in c
- Prev by Date: Re: stream io in c
- Next by Date: Re: stream io in c
- Previous by thread: Re: stream io in c
- Next by thread: Re: stream io in c
- Index(es):
Relevant Pages
|