Re: stream io in c
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 30 Jul 2008 18:35:48 -0700
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:
Please indent your code. Run it through "indent -kr" if necessary
(and if you have the "indent" program).
#include <stdio.h>
int main(void)
{
FILE *fp;
char name[]="text58.txt";
Ok, so far, so good.
fp=&name;
What?
fp is a FILE*, something you use for things like fopen. You're
assigning the address of a character array to it. What exactly is
"fp=&name;" intended to accomplish?
Just delete that line.
int c;
if ((fp = fopen(fp, "rb")) == NULL)
And here you're trying to use fp both to hold the result of fopen and
to hold the name of the file.
A FILE* and the name of a file are two entirely different things. You
pass the name of a file to fopen, and it gives you a FILE*. (A FILE*
is a pointer to some blob of information; you don't need to know the
details, which will vary from system to system.)
You already have a variable that holds the name of the file. Use it.
... fp = fopen(name, "rb") ...
{
printf("can't open %s\n", fp);
Mis-using fp again. You want name. And consider printing the error
message to stderr rather than stdout (that's not a high priority).
return 1;
Add "#include <stdlib.h> and change this to "return EXIT_FAILURE;".
The value 1 isn't necessarily meaningful.
}
else
{
for (c = 0; c <= 255; c ++) {
putc(c, fp);
You opened fp as an input file. Now you're trying to write to it.
If you wanted to write to the file, you should have used mode "wb"
rather than "rb".
}
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. I can't find an example in K&R where they hard-code
a filename like this, so I'm a little stuck.:-(
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: stream io in c
- From: Ron Ford
- Re: stream io in c
- 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
|