Re: Write to file
- From: Bryan <Bryan.Williams@xxxxxxxxxxxxx>
- Date: Wed, 13 Jun 2007 13:34:23 -0000
On 13 Jun, 13:04, Bill <bill.war...@xxxxxxxxxxxx> wrote:
Thanks alot. I'm making progress. No errors now, but the output in
the file is screwy. The code:
#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 pointer */
FILE *outfile;
char *p;
int c;
unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
Initialising this would be advisable, given what you're going to do
with it later.
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");
while((c = fgetc(pfile)) != EOF) /* Continue until end of
file */
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
This needs to be changed to :
buffer[count++] = (unsigned char)c;
As otherwise you are reading the first character in the while loop and
then ignoring it and reading a second character here. This is why you
get every other character in the output.
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 */
This is what's causing your output file to look a bit odd. Once you've
read the input up to a full buffer, you just print it to screen and
then re-start at the beginning of the buffer. This data never (except
for the last couple of rows, by chance) goes to the datafile and so
you don't get everything that you're after. So you either need to
output it here or store it somehow.
if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()=='E') /* Wait for Enter */
return 0; /* E pressed */
}
}
/* 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 */
//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");
if(outfile==NULL)
{
return 0;
}
for(count = 0; count < sizeof buffer; count++)
You also don't know that all of buffer has been filled. It's not
initialised so it could well be causing undefince behaviour here. You
either need to keep a count of how many characters you've read or need
to initialise the array and/or populate and look for '\0' characters
at read time.
Given what you're trying to do I would suggest you take a look at the
fgets function.
{
if(!isprint(buffer[count]))
{
buffer[count] = '.';
}
else
{
fputc(buffer[count],outfile);
}
And this lot doesn't need to be in an else, it needs to happen every
time.
<snip rest of code>
.
- References:
- Write to file
- From: Bill
- Re: Write to file
- From: Ben Bacarisse
- Re: Write to file
- From: Bill
- Write to file
- Prev by Date: Re: Write to file
- 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
|
|