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!
|