Read source file and target file name and copy the file - C File

C examples for File:File Operation

Description

Read source file and target file name and copy the file

Demo Code

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

#define SLEN 81//  ww w  .j  a va  2 s  .  c  o m

void get(char * string, int n);

int main(void){
  FILE *in;
  FILE *out;
  char source[SLEN];
  char dest[SLEN];
  int ch;
  unsigned int count = 0;

  printf("Enter a file to reduce: ");
  get(source, SLEN - 5);

  if ((in = fopen(source, "r")) == NULL){
    fprintf(stderr, "Could not open file %s.\n", source);
    exit(EXIT_FAILURE);
  }

  strcpy(dest, source);
  strcat(dest, ".red");
  if ((out = fopen(dest, "w")) == NULL){
    fprintf(stderr, "Could not open file %s.\n", dest);
    exit(EXIT_FAILURE);
  }
  while ((ch = getc(in)) != EOF)
    if (count++ % 3 == 0)
      putc(ch, out);

  if (fclose(in) != 0)
    printf("Error in closing file %s.\n", source);
  if (fclose(out) != 0)
    printf("Error in closing file %s.\n", dest);

  return 0;
}

void get(char * string, int n){
  fgets(string, n, stdin);

  while (*string != '\0')  {
    if (*string == '\n'){
      *string = '\0';
      break;
    }
    string++;
  }
}

Result


Related Tutorials