khuang8493
9/24/2018 - 12:27 AM

Put a char into the file using fputc()

How to use fputc()

fputc()

Writes or appends a character to the pointed-to file. 

* The file must be open only for "w" or "a". If not will suffer an error.

fputc(<character>, <file pointer>);

fputc('A', ptr2); // This will write character 'A' into the file with the file pointer "ptr2".


A practical use of fputc():

Using a loop we can use "fgetc()" to copy one file to another.

	char ch;
	while ((ch = fgetc(ptr)) != EOF)
		fputc(ch, ptr2);

So in this line of code, we first read a char from file pointer "ptr", and then if it's not the end of the file, put this char into file pointer "ptr2".

Another Linux command "cp" does this. If we type "cp filename1 filename2", we will copy the content of "filename1" to "filename2".