Solved

Replacing comma, poing, double point of a text file

Posted on 2004-03-20
13
358 Views
Last Modified: 2010-04-15

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

0
Comment
Question by:j79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10642237
Hi j79,
This smells like homework. I'm sure you're smart enough to figure it out yourself, as it is very simple.

Cheers!

Stefan
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10642929
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.

0
 
LVL 2

Expert Comment

by:Avik77
ID: 10644580
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))!=EOF)
  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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:j79
ID: 10644905
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
0
 
LVL 2

Author Comment

by:j79
ID: 10647910

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 &quot; or € with &euro; on .html pages?
Still homework?

j79


0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10648049
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)

0
 
LVL 2

Author Comment

by:j79
ID: 10649131

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
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10649222
Yeah,u can still give all the points to him.
Just trying to help out.
0
 
LVL 2

Accepted Solution

by:
Avik77 earned 250 total points
ID: 10650805
Hi j79,
       tough time for both us... Anyway I am happy that my suggestions were at least some use to u. I just wanted to guide u by giving a "very little piece of code" which has been unfortunately edited. Good luck.

Hi sunnycoder,
             I am aware of the EE membership agreement and have read it once more. Anyway I still feel that answering to his question was impossible by discussing it without giving a little bit of coding assistance.

Avik.
0
 
LVL 2

Author Comment

by:j79
ID: 10655642

I want to thank them who gave their assistance.

j79
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10659762
Thanks to everybody.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding a good hash function 4 132
An API detour question 7 106
placing a checked in the checklistbox based on the value in database table. 3 73
windows 10 pro lost profile. 10 83
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question