Replace vowels in file with *

This is a discussion on Replace vowels in file with * within the C Programming forums, part of the General Programming Boards category; Hi,
I have to write a program that replaces all vowels in a given file with *. Here's my code:
...

Because decreasing the file pointer is not going to do what you think it does and thus you can't just "replace" the characters in the file. You will have to read the whole file in and spit it out with the necessary changes.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

In this case, since a single character is being replaced by another single character, writing in-place is possible. Granted, fp-- is not the way to do it, but you can use fseek() to seek backward one character and write out the asterisk. After this, you must call fflush() before your next call to getc().

Also, c should be an int, not a char. getc() returns an int so that EOF can be distinct from all character values.

Arbitrary seeks with an offset only work predictably for files opened in binary mode. However, you can save the previous position prior to reading a character, test the character, then jump back and overwrite if it's a vowel:

>I would call that a bad idea as it trashes the original file.
>In general it's smarter to write a second file, delete the first, rename the second...
I hate to be the bearer of bad news, but your solution doesn't address your complaint. The original file will contain the new contents, either because the file was actively overwritten, or because it was deleted and replaced.

>This program works. The question is solved.
Except the part where your code is actually broken. Let's consider why:

getc returns int for a reason. EOF is a negative value, and if vanilla char is unsigned, you've got issues.

>I would call that a bad idea as it trashes the original file.
>In general it's smarter to write a second file, delete the first, rename the second...
I hate to be the bearer of bad news, but your solution doesn't address your complaint. The original file will contain the new contents, either because the file was actively overwritten, or because it was deleted and replaced.

It addresses the problem of an error mid-file... your software can bail without trashing the original file. It may not be much of an issue in this simple exercise but consider the risk when processing hundreds of megabytes of financial data... good habits are best formed early.

In any case, I got no bone to pick, the OP solved the problem in a way that he seems happy with... neither your solution nor mine.