C-style string problem

We are doing a project in Cygwin/g++ using dynamic arrays and command line arguments. The objective is to be able to enter in the command line like this:

./program <file_name> <search_word> <replace_word>

I am totally stuck on the replacement. I understand that you can use strstr to return a pointer to the first location of your searched string, but I do not understand how to implement that into replacing the word within my array.

Here is my current source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int length(char[]);
void read(char[], char[]);
int replace(char[], char[], char[], char[], int);

int main(int argc, char* argv[])
{
   int size = length(argv[1]); //size variable created to avoid calling function each time needed

   char *origContent = new char[size];//dynamically create an array based off # from length function
   char *modContent = new char[size*2];//dynamically create an array double the size of first array
   read(argv[1], origContent);
   replace(origContent, modContent, argv[2], argv[3], size);


   delete [] origContent;
   delete [] modContent;
}

int length(char fileName[])
{
   char c;//character to be read into
   int count;//counter for # of file elements

   ifstream sampleFile;
   sampleFile.open(fileName);//open the file

   while(sampleFile.get(c))
      count++;

   sampleFile.close();

   return count;

}

void read(char fileName[], char fileContents[])
{
   ifstream sampleFile;
   sampleFile.open(fileName);

   for (int i = 0; !sampleFile.eof(); i++)
      sampleFile.get(fileContents[i]);
}

int replace(char origContent[], char modContent[], char searchWord[], char replaceWord[], int size)
{

   char * stringLoc;
   stringLoc = strstr(origContent, searchWord);//stringLoc points to the very first occurence of the search word in the origContent array

}//I need to know how to use stringLoc to replace the words in origContent!


I am so stumped any help would be appreciated, thanks. I understand that stringLoc will have the first memory address of the first occurrence of my search word, but I do not know how to formulate that into a statement that will replace the search word with the replace word in origContent.
There's a much better way to get the length of a file: stat().
http://www.freebsd.org/cgi/man.cgi?query=stat&apropos=0&sektion=2&manpath=FreeBSD+10.0-RELEASE&arch=default&format=html

replace() won't work because replaceWord can be longer than searchWord. If that is the case, the fixed up text will be longer than the original text. So you need some way of growing the buffer or updating the file or something; it's not clear on where the replacement is done.
Thank you for your reply. Although this might sound strange, our instructor lays out a "map" of the each function and arguments required to be implemented in our programs. Although id much rather not have a whole function dedicated to finding the length of the file, I'm simply taking the suggestion from my instructor. He even mentioned to use a counter to find the size of the file. He has been saying all week that we will need to use strstr or strcmp in our replace function, and <string> absolutely cannot be included.
Also, we are aware that the strings can be a different length, we are supposed to build code around it. I am just totally stumped because I don't even know how to start replacing! Another instructor told one of my peers that an if-else statement comparing the origContent and searchWord arrays is possible, but I just don't see it.
He has been saying all week that we will need to use strstr or strcmp in our replace function
Ok, it sounds like you are expected to read the entire file in memory, then do the string replace on that.

The case you need to consider is when the replace string is larger than the search string. This is tricky because the final string will be larger. You can't guess how much larger a priori. That means, the replace function must either return the new string or be able to grow that buffer.

You should indicate read-only parameters by passing them as const. For example:
 
int length(const char fileName[])
Topic archived. No new replies allowed.