Re: Write to file



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).

.



Relevant Pages

  • Re: Array pointers
    ... Erik Max Francis wrote: ... >> results in a compiler warning). ... What's not an int *? ... The adress of the first element of the two- ...
    (comp.os.linux.development.apps)
  • [PATCH] handle scsi_add_host failure for aic7xxx and fix compiler warning
    ... Also silence a compiler warning: ... More majordomo info at http://vger.kernel.org/majordomo-info.html ...
    (Linux-Kernel)
  • [PATCH] fix compiler warning in fixed.c
    ... Correct the following compiler warning (and warnings resulting from ... * This is used for updating internal mii regs from the status ... static int fixed_mdio_register_device ...
    (Linux-Kernel)
  • fix compiler warning in ip_nat_standalone.c
    ... i just made small patch that fixes a compiler warning: ... const struct net_device *out, ... int ) ... +#ifdef CONFIG_XFRM ...
    (Linux-Kernel)
  • Re: Urgent C Questions
    ... int, not void. ... Then you need to turn up the warning level on QuickC. ... 3  missing prototype for 'main' ... stdio.h contains the declaration for printf: ...
    (comp.lang.c)