I am writing a c program to copy content of one text file to another but with the copied lines in the destination file preceded by the length of the line and a blank space. Let me illustrate my expectation:
original.txt destination.txt
daniweb 7 daniweb
forum 5 forum
plz help me! 12 plz help me!
Here is my work but I only could try copy its content from file to file. I cannot find out which function help me print number before each line. I thought it is easier for me if number is printed at last of line. Plz I need your hints. I love you all. I also examine using rewind, but it totally failed.
/*
* question07.c
*
* Created on: Apr 7, 2010
* Author: phathuy
*/
#include <stdio.h>
#include <stdlib.h>
void a_funct(char *, char *);
int main(void) {
char Org_File_Name[20];
char Dst_File_Name[20];
//FILE *fp;
puts("Enter a file name to read from: ");
scanf("%s", Org_File_Name);
puts("Enter a file name to copy data to: ");
scanf("%s", Dst_File_Name);
a_funct(Org_File_Name, Dst_File_Name);
return EXIT_SUCCESS;
}
void a_funct(char *a, char *b) {
FILE *fp, *fw;
int c, char_per_line = 0;
fp = fopen(a, "r");
if (fp == NULL) {
perror("Error opening such file ");
exit(1);
} else {
if ((c = fgetc(fp)) == 10) {
puts("No data found!");
}else{
fw = fopen(b, "w+");
do {
if(c != '\n') {
char_per_line++;
fputc(c, fw);
}else{
fputc(c,fw);
//rewind(fw);
//fputc(char_per_line, fw);
char_per_line = 0;
}
}while((c = fgetc(fp)) != EOF);
}
}
}