I want to build a function that reads text from a text file, surrounds all the words with '`', puts every word on a single line and saves the file to another one. I'm a beginner and I ended up with this code. It just opens the file, and puts the words on single lines...
Code:
void replace() {
char ch;
FILE *fp,*fp2;
fp = fopen("C:\\test.txt", "r");
if (!fp) {
printf("\nFile test.txt cannot be opened.\n");
}
fp2 = fopen("C:\\test2.txt", "w");
if (!fp2) {
printf("\nFile test2.txt cannot be opened.\n");
}
while((ch=getc(fp))!=EOF) {
if(isspace(ch)) {
ch='\n';
}
fputc(ch,fp2);
}
fclose(fp);
fclose(fp2);
printf("''test2.txt'' was created.\nPress Enter to go back to the Menu...");
getchar();
system("cls"); //*I know I shouldn't use system("cls"), but let's not comment it now.. :) *//
main(); //*returns to main(), which has a menu *//
exit(0);
}
How can I do that word formatting? Any ideas?
P.S. An example:
If the text file has:
Code:
The Quick Brown Fox
I want the new file to be:
Code:
`The`
`Quick`
`Brown`
`Fox`