Click to See Complete Forum and Search --> : Help with small c++ program I want build


kbug
07-22-2001, 10:15 PM
Hi Ive been learning c++ for 2 weeks now and Ive found a little prog I need that would actually be usefull to me.

What I need is a program that will go through
an html file and replace all of certain character(like a !! for instance) with strings of text out of another text file preferably randomly.

Was wonderin if anyone could just give me some advice on just how/where to start.

Have been lookin into fstream.h maybe something else would be better for this??

bdg1983
07-22-2001, 11:12 PM
I'm not sure exactly what you're asking, but here's what I'd do:

Open the file with the lines to be randomly assigned for reading, and read the whole thing into a vector (vector.h). Close this file, and open the one with the text and stuff to be replaced for reading, and another for writing (or just send them to stdout if you want), and every time you come to your special flag, just grab a random element from the vector and output that instead.

Stuka
07-22-2001, 11:37 PM
Bradmont's got a good overall plan - and yeah, you'll need fstream. It has your file I/O classes (ofstream, ifstream, fstream), plus some handy methods (getline()) for reading in.

kmj
07-23-2001, 08:50 AM
<shameless> you can go to www.codeexamples.org (http://www.codeexamples.org) to see some examples of how to use fstream to open, read, and write files, among other things. :)</shameless>

kbug
07-23-2001, 04:35 PM
Thanks guys ill try that

kbug
07-23-2001, 08:12 PM
aaarrrgh...this is turning out to be a nightmare seems its a bit hard to get c++ to work with text.

does anyone have any other ideas???

:confused: :confused: :confused: :confused: :confused: :confused: :confused:

jemfinch
07-23-2001, 11:33 PM
C++ isn't the first language I'd pick for manipulating text.

Consider using Python or Perl for this task, because they're much better suited for it.

Jeremy

kmj
07-24-2001, 10:04 AM
I was going to say the same thing originally, but I figured I'd hold off, since some people seem to get annoyed when you suggest a different language. In this case, jemfinch is right, no doubt. Take a look at python or perl; they're both relatively easy to learn and use, and both should be quite good for what you want to do.

kbug
07-24-2001, 02:27 PM
I tried to use something called gawk (dnloaded a book and everything) but no luck.

So which one is easier perl or python, Ive heard good thing about python and sounds like perl is a dying language :confused:

tecknophreak
07-24-2001, 03:51 PM
#include <fstream>
#include <stdlib.h>
using namespace std;

int main() {
int x(100), y(100); //I don't know how big you want the text and how many you have
char buff[x][y], bad('a); // whatever you want to replace
char line[1000]; // overkill but will get a long line
ifstream is("randomfile.txt");
while(is.getline(buff[cnt++], y)) {}
is.close();
is.open("oldhtml.html");
ofstream os("newhtml.html");
while(is.getline(line, 1000)) {
for (int i = 0; i < 1000 && line[i] != '\0'; ++i) {
if (line[i] == bad)
os << buff[rand() % x];
else
os << line[i];
}
os << endl;
}
}


May work, may not probably not. No guarantees or promises. I know, I know chars, eh. It also puts the output into newhtml.html instead of oldhtml.html.

#include <stdio.h> //??
system("rm oldhtml.html");
system("cp newhtml.html oldhtml.html");
system("rm newhtml.html");


I just wanted to show y'all that it could be done in c++. It's pretty small too. Let me know what you think.

Don't forget to put the right values in for x n y.

[ 24 July 2001: Message edited by: tecknophreak ]

Stuka
07-24-2001, 05:24 PM
OK - my lame attempt in C++ ;)
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main(){
ifstream infile("file.html"); //input file
ofstream outfile("newfile.html"); //output file
ifstream repfile("rand.txt"); //replacement strings

vector<string> random; //holds random text
string temp; //for loading the vector
int rand_size = 0, location;

//Load vector from random file (one string per line)
while(repfile){
repfile.getline(temp, 500) //max line = 500 chars
random.pushback(temp);
rand_size++;
}

while(infile){
infile.getline(temp, 500);
while(location = temp.find("!!") != basic_string::npos){
temp.replace(location, 2, random_string());
}
outfile << temp;
}
return 1;
}
Oh, the random_string() function is just to pick a random string from the vector - I'm not writin' that today. And of course error checking would be good :D

[ 24 July 2001: Message edited by: Stuka ]

[ 24 July 2001: Message edited by: Stuka ]

tecknophreak
07-24-2001, 08:13 PM
Ah yes, I rememeber liking vectors and all the nice functions they have. I have to get out of this fscking C environment and get back into C++, which is much nicer.

Wouldn't that just place 2 chars into the string instead of a whole string?
Oh, right, mine's not set up for more than one char. hmmm, go fix.

[ 24 July 2001: Message edited by: tecknophreak ]

tecknophreak
07-24-2001, 08:19 PM
char tmp[2];
while(is.getline(line, 1000)) {
for (int i = 0; i < 1000 && line[i] != '\0' ;) {
tmp[0] = line[i];
tmp[1] = line[++i];
if (!strcmp(tmp, "!!")) {
os << buff[rand() % x];
++i;
}
else
os << line[i];
}
os << endl;
}


ok tired now. no more code for night bye.

Stuka
07-25-2001, 09:41 AM
Well, it shouldn't - unless the version I looked at the docs for is MS specific (all I have at work). It says that the replacement is the operand sequence supplied by the string argument. I'm assuming this would mean the whole string, but I'm not sure...and I'm too lazy to test it.

kbug
07-25-2001, 09:33 PM
Thanks fdor the help guys :) :)

sans-hubris
07-25-2001, 11:42 PM
Just realize that much of the code above (from just a glance) is really buggy and needs a lot of fixing, but the ideas are all correct. I don't have time ATM to mention all of them.

Stuka
07-26-2001, 12:22 PM
Of course it's buggy - I'm sittin' here at work talkin' on the phone, tryin' to hack out some STL code on the fly, without testing! :D I ain't that good yet!

kbug
07-26-2001, 03:40 PM
Ive actually managed to pull this off in python quite easily(hadnt touched it till 2 days ago which says alot bout the language).

So im actually wonderin whether to carry on learning c++??? :p