Link to home
Start Free TrialLog in
Avatar of Caro0101
Caro0101Flag for Canada

asked on

URGENT! I need help with this function..beginner

I need to read a char from a file. If char is equal to the first char of the string that add one to counter. Read next char with the second char of your search string. If char not equal reset counter to 0 and move char into ostream .if equal to the length of your searchString than found the string in the file.
call a function to find out what needs to be replaced by that special string.

#include<fstream.h>
#include<iostream.h>

CString searchstring = "<!--$$(";
int len = searchstring.Getlenght();
ifstream in("page1.htm");
assure(in,"page1.htm");
ofstream out("temp.htm");
assure(out, "temp.htm");
int counter = 0;


I'm not sure how to do this could someone give me an example...Remember this is html so I'm not sure if '\0' are present...

Thank you in advance




 
       
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasonclarke
jasonclarke

All you seem to want to do is replace matching text in the input file with something else in the output, you can use CStrings find method to help you.  There should be no nulls in the input since html files are simply text files.

What your algorithm states is something a bit different, it says if you have a partial match, throw away characters in the partial part of the match.

So, say you had:
Text<!--Something

this would be replaced by:

TextSomething

Is this what you want? KangaRoos program assumes you want anything other than the search string to be passed unchanged to the output (which I assume is what you want).

Here is a program that uses find to do what I think you want.

#include <afx.h>

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

CString Match(const CString& seq)
{
    return "REPLACEDAGAIN";
}

CString AnnotateLine(const CString& line, const CString& seq)
{
    CString result = "";
    CString input  = line;
    while (input.GetLength() > 0)
    {
        int pos = input.Find(seq);
        if (pos >= 0) {
            CString prefix = input.Mid(0,pos);
            CString match  = input.Mid(pos,seq.GetLength());
            CString postfix= input.Mid(pos+seq.GetLength(),input.GetLength()-prefix.GetLength()-match.GetLength());
            result += prefix + Match(seq);
            input = postfix;
        } else {
            result += input;
            break;
        }
    }
    return result;
}

void main()
{
    CString searchstring = "<!--$$(";
    int len = searchstring.GetLength();
    ifstream in("..\\page1.htm");
    ofstream out("temp.htm");
    int counter = 0;

    string line;
    while (!in.eof())
    {      
        std::getline(in,line);
        CString result = AnnotateLine(line.c_str(),searchstring);
        out << (const char*) result << endl;
    }
}


Avatar of Caro0101

ASKER

For example;

<HEAD>
Good morning
<!--$$(mystring)$$--!>
</Head>

What I want is to read every characters and compare them to my special string "<!--$$(" when I have reached this I want to parse what is in the () and put it in a temp variable and call a function that will tell me what this does. I also want to remove <!--$$(mystring)$$--!> meaning not incorporate it into out(ofstream).

There will be more than one occurence of this special string.

Does this help a bit more in what I'm trying to accomplish

Thank you in advance for your help
Try this:

#include <afx.h>

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

CString Match(const CString& match)
{
    return "<h1>Some HTML</h1>";
}

CString AnnotateLine(const CString& line, const CString& beginmarker,
                               const CString& endmarker)
{
    CString result = "";
    CString input  = line;
    while (input.GetLength() > 0)
    {
        int pos = input.Find(beginmarker);
        if (pos >= 0) {
                  int endmatchpos = input.Find(endmarker);
                  if (endmatchpos == -1)
                  {
                        // We have an error, prefix with no postfix
                        throw 1;
                  } else {
                        int size = input.GetLength();
                CString prefix = input.Mid(0,pos+beginmarker.GetLength());
                        int matchStart = pos + prefix.GetLength();
                        int matchSize  = endmatchpos - matchStart;
                CString match  = input.Mid(matchStart, matchSize);
                CString postfix= input.Mid(endmatchpos+endmarker.GetLength(),size);
                        result += Match(match);
                input = postfix;
                  }
        } else {
            result += input;
            break;
        }
    }
    return result;
}

void main()
{
    CString beginmarker = "<!--$$(";
      CString endmarker = ")$$--!>";
    ifstream in("page1.htm");
    ofstream out("temp.htm");
    int counter = 0;

    string line;
    while (!in.eof())
    {
        std::getline(in,line);
        CString result = AnnotateLine(line.c_str(),beginmarker,
                                            endmarker);
        out << (const char*) result << endl;
    }
}

it gets a bit messy in the function there, but you can tidy it up.  

Note: this won't cope with the case where the thing to be replaced spreads over multiple lines.

(BTW this sort of thing is much easier with Perl).