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);
		}
	}
}

use fgets to read from the first file
use fprintf or fputs to write to the second file.

here's the most basic example. error checking and user input not included.

readhandle = fopen(readfilename,"r")
writehandle = fopen(writefilename,"w")

while ( fgets(readbuffer, MAX_LINE_LENGTH, readhandle) != NULL )
   fprintf(writehandle, "%d %s", strlen(readbuffer), readbuffer);

fclose(readhandle);
fclose(writehandle);

Thank you so much.

oops i always get this damn thing wrong... sorry, i never get user input from terminal screen at work, its always through guis.

the while statement should continue as long as return of fgets() is >0, not for just != NULL ... so try this instead:

readhandle = fopen(readfilename,"r")
writehandle = fopen(writefilename,"w")

while ( fgets(readbuffer, sizeof(readbuffer), readhandle) > 0 )
   fprintf(writehandle, "%d %s", strlen(readbuffer), readbuffer);

fclose(readhandle);
fclose(writehandle);

oops i always get this damn thing wrong... sorry, i never get user input from terminal screen at work, its always through guis.

the while statement should continue as long as return of fgets() is >0, not for just != NULL ... so try this instead:

readhandle = fopen(readfilename,"r")
writehandle = fopen(writefilename,"w")

while ( fgets(readbuffer, sizeof(readbuffer), readhandle) > 0 )
   fprintf(writehandle, "%d %s", strlen(readbuffer), readbuffer);

fclose(readhandle);
fclose(writehandle);

I could not understand what you do not want expect if you did not change the condition of while loop into > 0. I examine both cases, the result did not alter. Can you give an example to distinct these cases?

my mistake once again. the first way i showed is correct:

while ( fgets(readbuffer, MAX_LINE_LENGTH, readhandle) != NULL )

fgets will return NULL if the end of file is reached. i got mixed up with another function that returns EOF, which is a negative number, so i mistakenly thought i needed to change the condition to > 0 .

that second way will work, also, because the NULL pointer evaluates as zero. but the first way is technically the correct way.

sorry for the confusion. I'm at work, and i often post things in a hurry without taking care to verify that my memory is accurate.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.