Re: Write to file
- From: Old Wolf <oldwolf@xxxxxxxxxxxxxx>
- Date: Wed, 13 Jun 2007 17:19:02 -0700
On Jun 13, 10:28 pm, Bill <bill.war...@xxxxxxxxxxxx> wrote:
//causes an access violation error
fputc(outfile, isprint(buffer[count]) ? buffer[count]:'.');
Hint: the prototype for fputc is:
int fputc(int c, FILE *stream);
If you didn't get a compiler warning at least on this
line, you should really turn up your warning level.
If gcc is your compiler then use the switches:
-Wall -Wextra -pedantic
along with either -std=ansi or -std=c99.
//causes crazy output in file
/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/
This will fill the file with ones and zeroes
(bytes, not bits!) I suspect you wanted a bit
more substance to the third argument to fwrite.
A couple of other style notes:
printf("%c", isprint(buffer[count]) ? buffer[count]:'.');
putchar(X) seems nicer to me than printf("%c", X).
In fact, you use this ternary expression in several
places in the code so I would like to see it contained
as its own function, e.g.
int ch_printable(int ch) { return isprint(ch) ? ch : '.'; }
and then in your other functions:
putchar( ch_printable( buffer[count] ) );
Another way to go would be to make your function:
int fputc_printable(int ch, FILE *stream);
pfile = fopen ("my.txt" , "r");
while(!feof(pfile))
You should check pfile != NULL immediately after the fopen.
(Others have already explained the problem with feof).
.
- Follow-Ups:
- Re: Write to file
- From: Keith Thompson
- Re: Write to file
- References:
- Write to file
- From: Bill
- Write to file
- Prev by Date: Re: Can mystr[i]=mystr[ii] be a problem when i==ii
- Next by Date: Re: Write to file
- Previous by thread: Re: Write to file
- Next by thread: Re: Write to file
- Index(es):
Relevant Pages
|
|