j79
asked on
Replacing comma, poing, double point of a text file
Hello,
I'd need a small app, which asks for a text file when started, and replaces all , . : with words comma, point, double poing, question mark and so one. After replacing it should save/replace the file.
Thanks
j79
Yeah,this is quite simple.
Just a small suggestion.
Open another file in write mode,keep writing into this file as u read from ur original file.
When u encounter any of the characters u want to change,just write the new string in the other file.
If u want to replace the old file,delete it and rename the new file to the old file.
Just a small suggestion.
Open another file in write mode,keep writing into this file as u read from ur original file.
When u encounter any of the characters u want to change,just write the new string in the other file.
If u want to replace the old file,delete it and rename the new file to the old file.
just initialize --
char p[]=" point ", c[]=" comma ", dp[]=" double poing ";
as stated by ankuratvp open two separate FILE streams one for reading ur text file as read mode and the other as the destination file as write mode.
// hope this helps u a bit
#include<stdio.h>
FILE *source, *dest;
char p[]=" point ",d[]=" comma ", dp[]=" double poing ";
/* for other characters similarly provide seperate strings */
void main()
{
int c;
source=fopen("xyz.txt","r" ); // u can get that from any where
dest=fopen("xyz1.txt","w") ;// similarly this
while((c=fgetc(source))!=E OF)
switch(c){
case '.' : {fputs(p,dest);break;}
case ',' : {fputs(d,dest);break;}
case ':' : {fputs(dp,dest);break;}
default : {fputc(c,dest);}
}
fcloseall();
}
/* if u want to replace the existing file just include dos.h and
after fcloseall() write system("del xyz.txt");
and system("ren xyz1.txt xyz.txt");
that will serve ur purpose OF COURSE IF U R IN DOS */
Avik.
char p[]=" point ", c[]=" comma ", dp[]=" double poing ";
as stated by ankuratvp open two separate FILE streams one for reading ur text file as read mode and the other as the destination file as write mode.
// hope this helps u a bit
#include<stdio.h>
FILE *source, *dest;
char p[]=" point ",d[]=" comma ", dp[]=" double poing ";
/* for other characters similarly provide seperate strings */
void main()
{
int c;
source=fopen("xyz.txt","r"
dest=fopen("xyz1.txt","w")
while((c=fgetc(source))!=E
switch(c){
case '.' : {fputs(p,dest);break;}
case ',' : {fputs(d,dest);break;}
case ':' : {fputs(dp,dest);break;}
default : {fputc(c,dest);}
}
fcloseall();
}
/* if u want to replace the existing file just include dos.h and
after fcloseall() write system("del xyz.txt");
and system("ren xyz1.txt xyz.txt");
that will serve ur purpose OF COURSE IF U R IN DOS */
Avik.
ASKER
Thank you Avik77,
it seems to do it very good.
I only would need an input at starting for giving the text file to replace.
How to realize it?
j79
it seems to do it very good.
I only would need an input at starting for giving the text file to replace.
How to realize it?
j79
ASKER
Hello sunnycoder,
I'm 24 years old and has no homework since 6 years. You also can see the history of my profile for what "homework" I've asked/answered in the past.
I also has some years experience in java and web development, but not really in C. That's reason why I asked.
Only why my question is sounding like homework for you, it has not to be really a homework, or?
What would it be if I would ask how to replace special characters like " with " or € with € on .html pages?
Still homework?
j79
Hey j79,
Sorry for getting u wrong with the homework thing.
>it seems to do it very good.
>I only would need an input at starting for giving the text file to replace.
>How to realize it?
All u have to do is:
char fn[50];
char ch;
FILE *fp,*out;
scanf("%s",&fn); //get the entire filename including extension
fp=fopen(fn,"r"); //open the file for reading.
out=fopen("second.txt","w" );
//read char by char and write to the new file till EOF
//checking everytime for the chars u want to replace
fclose(out);
fclose(fp);
Here i am assuming that u have already got the code provided by avik77(which i cant see,i guess got edited)
Sorry for getting u wrong with the homework thing.
>it seems to do it very good.
>I only would need an input at starting for giving the text file to replace.
>How to realize it?
All u have to do is:
char fn[50];
char ch;
FILE *fp,*out;
scanf("%s",&fn); //get the entire filename including extension
fp=fopen(fn,"r"); //open the file for reading.
out=fopen("second.txt","w"
//read char by char and write to the new file till EOF
//checking everytime for the chars u want to replace
fclose(out);
fclose(fp);
Here i am assuming that u have already got the code provided by avik77(which i cant see,i guess got edited)
ASKER
Hello ankuratvb,
no problem. I have the code from Avik77 and I think he should get the most points because he posted the working code.
j79
Yeah,u can still give all the points to him.
Just trying to help out.
Just trying to help out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I want to thank them who gave their assistance.
j79
Thanks to everybody.
This smells like homework. I'm sure you're smart enough to figure it out yourself, as it is very simple.
Cheers!
Stefan