Hi All,

I need to write a code in C or C++ where some text in text_file_1 needs to be replaced with some other text which is in text_file_2.
both the text files are stored at some location Text_file_1 has text:
user,pass
jojo, beans
where jojo and beans need to be replaced with some other username and password which is stored in anothet text_file_2. Please help as I am new to C/ C++ and need this code urgently.

Member Avatar for MonsieurPointer

Please help as I am new to C/ C++ and need this code urgently.

Please understand that this forum does not exist to complete work for you - especially not in "urgent" cases. We can provide you with help, but not the solutions.

For this task I recommend that you look at the following functions:

  • fopen
  • fprintf
  • fgets
  • fclose

That should get you started.

Think about the flow of the program, and work it through with paper and pencil first.

Open the file, read the contents line by line with fgets(), until you reach the words to be replaced. All the lines are being written out to a second filename.

Acquire the new word that you will be adding, and put it into the char array you are using to hold the row of text with fgets(), at the correct position. Make any char adjustments that you need to make in the row of text now. (maybe the new word is longer or shorter than the old word).

Write out the new line of text to the temp file, and continue reading and writing the lines of text, until you have no more data.

Kill the old filename, and name the temp filename to the old filename.

This isn't "hello world" easy, but if you take it step by step, and visualize where the current position of the file pointer is as you are doing the replacement, it's not as difficult as it sounds.

If you need it coded up urgently, I suggest you simply hire someone to do it for you. C is not a language you get good at, overnight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.