Re: Write to file



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>


.



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Re: K&R exercise 1-18
    ... _without_ the string terminator and has MAXIMUM+1 array elements. ... char lineis in this context equivalent to char *line ... int main ... You lose one character by overwriting it with '\n'. ...
    (comp.lang.c)
  • Re: Write to file
    ... You can read the file one character at a time. ... compared to the current char. ... Once the (lgh - 1)th char ... void initnext(int *next, const char *id, int lgh) ...
    (comp.lang.c)
  • [KGDB PATCH][2/7] Serial updates, take 2
    ... Also make put_packet look at the char it reads, ... * Empty the receive buffer first, then look at the interface hardware. ... * This is the receiver interrupt routine for the GDB stub. ... -extern volatile int kgdb_connected; ...
    (Linux-Kernel)
  • Re: Write to file
    ... fwrite, outfile); ... This will repeatedly try to print the first character in buffer as ... int main(int argc, char *argv) ...
    (comp.lang.c)