Re: stream io in c



Ron Ford wrote:

<snip>

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


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

If you are using tabs to indent your code, consider switching to spaces,
since tabs are often stripped out by Usenet software.

int c;

if ((fp = fopen(name, "wb")) == NULL)
{
printf("can't open %s\n", fp);

The 's' type specifier expects a char* argument that must point at a
string. You have supplied it a FILE* argument, a certain route to
undefined behaviour.

return 1;

And one is not a portable return value. The portable values are 0,
EXIT_SUCCESS and EXIT_FAILURE.

Surely you must have noted these elementary things as they are often
mentioned in this group, besides being in it's FAQ.

}
else
{
for (c = 0; c <= 255; c ++) {
putc(c, fp);

Since you are writing raw byte values, you might consider changing the
misleading filename extension.

Also the maximum value of an unsigned byte in C is given by UCHAR_MAX.
It is not necessarily 256. And note that 'c' is declared as an int, not
unsigned char.

}
fclose(fp);
}
return 0;
}
// gcc -o chars mkchars2.c

This command is not a conforming implementation of ISO C. For that you
need:

gcc -ansi -pedantic /* For conformance to C90 */
gcc -std=c99 -pedantic /* For incomplete but good conformance to C99
*/

Also add the -Wall and -W flags for extra diagnostics which are always a
help.

<snip>

.



Relevant Pages