Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT! I need help with this function..beginner

Posted on 1999-07-06
4
Medium Priority
?
192 Views
Last Modified: 2010-04-02
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




 
       
0
Comment
Question by:Caro0101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 400 total points
ID: 1199303
CString temp;
while (!in.eof())
{
  char c;
  in >> c;
  if(c == searchstring[counter]) // assuming CString::operator[]
  {
      temp += c; // assuming CString::operator+=(char)
      ++count;
      if(count == len)
      {
         dofunction();
         count = 0;
      }
  }
  else
  {
    if(counter > 0)
    {
      out << temp;
      temp = "";
      count = 0;
    }
    out << c;
  }
}
// I've made some assumtions on CString
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199304
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;
    }
}


0
 

Author Comment

by:Caro0101
ID: 1199305
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
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199306
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).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question