Rugaloo
Rugaloo

Reputation: 131

My text for output file is not the same as input

#include <stdio.h>
#include <stdlib.h>

int main() {

  int c;

  FILE *poem = fopen("short.txt", "r");
  FILE *html = fopen("index.html", "w");

  if (poem == NULL){
      perror("Error in opening file");
      return(-1);   
  }
  if (html == NULL){
      perror("Error in opening file");
      return(-1);   
  }

  while((c = fgetc(poem)) != EOF) {
       c = getc(poem);
       fputc(c, html);
  }

  fclose (poem);
  fclose (html);
  return 0;
}

I've been searching and trying but I can't figure it out. My read file has less than a sentence of words, and then when it outputs it to index.html it's all jumbled up. I don't really understand know whats wrong with the code. Any help will be appreciated. Thanks!

Upvotes: 1

Views: 50

Answers (1)

pmg
pmg

Reputation: 108958

You are doing 2 reads for each write

  while((c = fgetc(poem)) != EOF) {  // read
       c = getc(poem);               // read
       fputc(c, html);               // write
  }

Upvotes: 2

Related Questions