|
-
Aug 23rd, 2008, 02:35 PM
#1
Thread Starter
Fanatic Member
Create File, Find and Replace
Hello everyone .
I'm new to the C++ world! Though I have a good dose of JavaScript in my experience (Syntax is somewhat similar). I know my code is not very good, but I'm doing my best .
I'm trying to create a program that Reads data from a file, finds all instances of text, and then replaces it with some other text.
It doesn't matter if the changed file is saved to another location, or if the old file is overwritten, but I would prefer if the old file was left intact.
Here's my code so far:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void doFiles(char sourceFile[], char destFile[], char findWhat[], char replaceWith[]);
string replaceString(char stringToSearch[], char findWhat[], char replaceWith[]);
int main()
{
// Template file, Destination File (Will be over-written), Find what, Replace With
doFiles ("c:\\test_file.txt", "c:\\tmp.txt", "C:\\wamp", CURRENTDIR[]);
cout << endl << endl << endl << endl << "Executing the webserver...";
// Execute Apache Code
return 0;
}
string replaceString(char stringToSearch[], char findWhat[], char replaceWith[])
{
// SADNESS HERE :(
return 0;
}
void doFiles(char sourceFile[], char destFile[], char findWhat[], char replaceWith[])
{
char str[65535];
ofstream myfile (destFile);
myfile << "";
cout << "Opening File: " << sourceFile << endl;
cout << "Writing File: " << destFile << endl << endl;
cout << "Replacing All Instances Of: " << findWhat << endl;
cout << "With: " << replaceWith << endl;
cout << "Begining Write: "<< endl << endl << endl;
fstream file_op(sourceFile,ios::in);
while(!file_op.eof())
{
file_op.getline(str,65535);
//REPLACE STR HERE
str << replaceString(str,findWhat,replaceWith);
if (myfile.is_open())
{
myfile << str << endl;
cout << str << endl;
}
else cout << "Unable to open file";
}
if (myfile.is_open())
{
myfile.close();
}
file_op.close();
cout << endl;
}
I've been jumping around the net for hours now trying to figure out this code! I always get errors such as can't convert char[] to chat[7] and what not. Mainly because I don't understand C++ that well yet. So I came here looking for help .
I've found a good version of a WAMP server, but the problem is that it requires a conf file to have the correct dir, or the program fails to start. So I decided to create this program to grab the current directory, and write it to the apache config file, then execute apache.
I've nearly given up and will just write it in VB.Net if I can't get it, but thought it'd be a good idea to learn C++ (Or at least start to).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|