Re: Write to file
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Wed, 13 Jun 2007 12:09:13 +0100
Bill <bill.warner@xxxxxxxxxxxx> writes:
Hello,
I'm trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone help out a newbie?
Sure...
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */
int main(int argc, char *argv[])
{
FILE *pfile;
FILE *outfile;
char *p;
unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */
char string [100];
pfile = fopen ("my.txt" , "r");
You should check it worked. I know you know this!
while(!feof(pfile)) /* Continue until end of file */
This will cause you some surprises and won't do what you think. You
should check the return from the read operation (in you case) fgetc:
int c;
....
while ((c = fgetc(pfile)) != EOF) {
...
The feof function only returns a non-zero value *after* an input
operation has failed due to and end-of-file condition. It should be
used only to tell *why* things have gone wrong.
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:'.');
printf("\n"); /* End the line */
count = 0; /* Reset count */
if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()=='E') /* Wait for Enter */
return 0; /* E pressed */
There getchar calls will normally be working on a buffered input so it
is not a good way to "pause" your program. If I wanted to do this (and
for some reason I don't these days) I'd write a function to read a
whole line of input and decide what to do based on that.
}
}
/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:'.');
}
printf("\n");
fclose(pfile); /* Close the file */
You should check if this worked also.
//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");
if(outfile==NULL)
{
return 0;
}
//causes an access violation error
for(count = 0; count < sizeof buffer; count++)
{
fputc(outfile, isprint(buffer[count]) ? buffer[count]:'.');
You should check that this worked. Get in the habit of checking
everything that there is to check!
}
//causes crazy output in file
/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/
This will repeatedly try to print the first character in buffer as
many times as there are printable characters in the buffer!. Also, by
using it this way (asking to write no data sometimes) you make error
checking much harder. Stick with the fputc method. It is fine. For
the record you probably intended:
fwrite(&buffer[count], 1, isprint(buffer[count]), outfile);
If you want to print a whole buffer. Process it first:
for (count = 0; count < sizeof buffer; count++)
if (!isprint(buffer[count]))
buffer[count] = '.';
if (fwrite(buffer, 1, count, outfile) != count)
/* report a write error... */
fclose(outfile);
getchar();
return 0;
}
--
Ben.
.
- Follow-Ups:
- Re: Write to file
- From: Bill
- Re: Write to file
- References:
- Write to file
- From: Bill
- Write to file
- Prev by Date: Write to file
- Next by Date: Re: Write to file
- Previous by thread: Write to file
- Next by thread: Re: Write to file
- Index(es):
Relevant Pages
|
|