Re: stream io in c



On Wed, 30 Jul 2008 18:35:48 -0700, Keith Thompson posted:

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.:-(

Thanks, Keith, I seem to be doing much better:


#include <stdio.h>
int main(void)
{
FILE *fp;
char name[]="text58.txt";


int c;

if ((fp = fopen(name, "wb")) == NULL)
{
printf("can't open %s\n", fp);
return 1;
}
else
{
for (c = 0; c <= 255; c ++) {
putc(c, fp);
}
fclose(fp);
}
return 0;
}
// gcc -o chars mkchars2.c

It seems to compile and behave:

C:\MinGW\source> gcc -o chars mkchars2.c

C:\MinGW\source>chars

C:\MinGW\source>dir
Volume in drive C has no label.
Volume Serial Number is 486B-CFF3

Directory of C:\MinGW\source

[...]
07/31/2008 02:58 AM 309 mkchars1.c
07/30/2008 06:31 PM 257 text55.txt
07/30/2008 06:56 PM 2,592 mkchars1.o
07/30/2008 06:56 PM 19,581 new.exe
07/31/2008 03:06 AM 312 mkchars2.c
07/31/2008 03:06 AM 256 text58.txt
07/31/2008 03:06 AM 17,203 chars.exe
27 File(s) 108,824 bytes
3 Dir(s) 492,433,408 bytes free

I think this shows a difference between a way to get a file with 257 bytes
(> .txt) and one without an ultimate -1.

I'll tune up the minor points when I can follow through. Best regards,

--
We are here and it is now. Further than that, all human knowledge is
moonshine. 3
H. L. Mencken
.



Relevant Pages

  • Re: stream io in c
    ... zero to 255, putchar'ing in the body of the loop, then redirecting output ... int main ... This compiles but gcc warns of incompatible pointer types. ...
    (comp.lang.c)
  • Re: stream io in c
    ... zero to 255, putchar'ing in the body of the loop, then redirecting output ... int main ... This compiles but gcc warns of incompatible pointer types. ... This is because you try to open it for reading and, presumably, the ...
    (comp.lang.c)
  • Re: data types
    ... Usually short is smaller than an int. ... Your compiler is either *really* old or broken or both. ... recommend either a version of gcc,, or Visual Studio ... Microsoft's Visual Studio Express has an onerous EULA). ...
    (comp.lang.c)
  • Re: gcc bug? Openoffice port impossibel to compile on 4.8
    ... remove this silly "bitten by the Linux bug" and the red-herring of gcc ... struct bar {int a; int b;} dapper; ... The *warning* emitted by gcc when enough analysis is done (e.g. ...
    (freebsd-hackers)
  • Re: [PATCH 3/7] PCI PM: Fix saving of device state in pci_legacy_suspend
    ... so the icache is going to scream but gcc folks know ... I'll see Michael what exactly the situation is here and if we ... int smp_request_message_ipi ...
    (Linux-Kernel)