Re: stream io in c
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Thu, 31 Jul 2008 15:04:37 +0530
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>
.
- 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
- Re: stream io in c
- From: Keith Thompson
- Re: stream io in c
- From: Ron Ford
- stream io in c
- Prev by Date: Re: stream io in c
- Next by Date: Re: $ & _
- Previous by thread: Re: stream io in c
- Next by thread: Re: stream io in c
- Index(es):
Relevant Pages
|