Thread: How to replace strings in a file?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    How to replace strings in a file?

    I'm really lost in this problem. I'm given a text file that has 5 different 'special characters' that need to be replaced. The other text file consists of the 5 special characters, and the words that will replace them (all on their each line).

    So I have to scan the first file and find all the special characters and replace them with the corresponding words in the 2nd file.

    What function calls should I use to find and replace the characters?

    Sorry if this is not clear, it's late and I've been *trying* to code other programs all night.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Get an array of the words that needs to be replaced, and what they need to be replaced with. Read in the other file and as you read it, out put it to a temp file. Before your print to temp file, check to see if one of the special characters is in the output buffer, and then instead of printing that, print what tis' supposed to be.

    Hopefully that quick and dirty thing will help you out. It is pretty inefficient, but should get the job done.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Here some function that you can use:

    fopen
    fgets
    strtok
    strchr
    fprintf
    fclose

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Thx for the replies, I'll try 'em out after classes.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Ok, I've got a question on the fgets function.

    Say I want to read a line from a file that is not at the beginning. How do I specify the fgets function to read lines n - m?

    What I have so far is an array of size 5. Now I want to store, say the words starting from line 5 of a file to line 10.

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    If you wish to read lines from 5 to 10, then you call fgets() 4 times and ignore the data read here (if not required). From the 5th call to the 10th call to fgets(), you have the lines you wanted.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    OK, thanks, I was actually thinking about doing that this morning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. reading from a file + list of strings
    By stewie1986 in forum C Programming
    Replies: 2
    Last Post: 12-06-2007, 11:59 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Writing strings to file
    By Ichmael™ in forum C++ Programming
    Replies: 6
    Last Post: 07-12-2005, 12:37 PM