LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2004, 09:34 PM   #1
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Rep: Reputation: 30
search and replace C code


I'm trying to find a way to search and replace words in a file. I figured i'd try fseek() to help me but I'm still unsure of how to do it. Could I get your input/links.
 
Old 05-18-2004, 12:27 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
GNU Search and Replace gsar has already done it -
search and replace is a somewhat complex thing.
You can download the source here to see how it works -

http://www.jsiinc.com/SUBL/tip5800/rh5801.htm
 
Old 05-18-2004, 07:36 PM   #3
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
Hmm, what if I was to make the program look at the text and then output the changed text to a file?



Ok, to understand better, I'm trying to create a intel asm to at&t asm syntax changer. The program will basicly grab intel asm source code and change all of it's code into at&t syntax. So, I need to change all the registers from reg to %reg and change instructions as well.

So basicly, I'm trying to find a way to do this with less effort possible. So my program should :

1.Take first line
2.Examin first line
3.Change what needs changing
4.Go to line++
5. End of line? yes quit, no continue to 6.
6.Go to 1
 
Old 05-18-2004, 11:11 PM   #4
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
I wrote some code, not sure if it works, what's your opinion?

Code:
int inChar,location=0;
while ((inChar=getc(infile)) != EOF) {
	if ((inChar=getc(infile)) != " ") { //  If not a space
                       takethis[location] = inChar;  // put the character in takethis
                       takethis[location+1]='\0'}    // add 1 and add a end of line
                       location++;                       // keep going through the list untill we get
                                                             // a space
	for (i, i<12, i++) {                                         
		if ( strcmp(takethis, lookup[i]) == 0 ) {        // at space, take what we had
				sprintf(fileout, replace[i]+" ");     // look it up in lookup list
		} // end if                                               // see if it needs replacing and 
	} // end for                                                     // do it

	location++; // needed so I can keep going after the space
} // end while
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace in every file Thinking Programming 3 07-30-2005 05:50 PM
Python search and replace Accordion Programming 1 02-22-2005 07:54 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration