-1

I'm looking for some help for my new project and I'm absolute new in programming in C++.

I've a html template (size ~ 8kb) with some placeholders inside. My task is to read this template edit the placeholders and add some or less containers depending on my source.

I tried different ways but always get stucked on a different point.

1:

void readHTML1()
{
    stringstream text;
    ifstream in_file("index.html");

    text << in_file.rdbuf();
    string str = text.str();
    string str_search = "[CREATE_DATE]";
    string str_replace = "12:19 24.05.2022";
    size_t pos = str.find(str_search);
    str.replace(pos, string(str_search).length(), str_replace);
    in_file.close();
    
    ofstream out_file("index_edit.html");
    out_file << str;
}
void readHTML2() {
    FILE * stream;
    stream = fopen("index_edit.html", "r+");
    char line [1000];

    if (stream) {
        if (fgets(line, sizeof(line), stream) == NULL) {
            printf("fgets error\n");
        } else {
            fseek(stream, 31, SEEK_CUR);
            char input[] = "hugo";
            fwrite(input, 4, 1, stream);
        }
        fclose(stream);
    }
}
  1. The replace of the placeholder works fine, but only a part of the whole stream (html file) will be stored in the "out_file". The rest get lost. I speake about a file size of 8kb but it seems to me that is not the problem.

  2. The output is "complete" so the whole stream will be written. But replacing the placeholders overwrites some parts that I don't want to. Is the placeholder smaller then the new value, the rest signs after the placeholder overwrites parts after the placeholders.

I prefere the first variant because it is easier for me to replace the placeholders with the new inputs. But I hope you can help me to understand my first, maybe faulty, steps in this new world (for me). Thank you for your time. I appreciate it. I'm ready to learn ...

Latviel
  • 1
  • 3
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jun 08 '22 at 11:00
  • Cannot reproduce the issue you see with the first code block. Works as expected for me. – Botje Jun 08 '22 at 11:03
  • Of course I had already used the debugger. Point 1 is all right the str variable is filled with all the data, but it seems that the operator does not return the string in the same way as I would like. This solution is not directly from me, but I am trying to learn. And in point 2, I'm not sure. Did I use the wrong method or do I not understand how to use streams (allocate memory)? – Latviel Jun 08 '22 at 11:12
  • @Botje maybe the signs are limited!? I've nearby 7400 signs in the html. – Latviel Jun 08 '22 at 11:17
  • I tested your code with a 2.4 MB file. – Botje Jun 08 '22 at 11:17
  • Maybe I've missed to include something? #include "stdafx.h" #include "iostream" #include #include using namespace std; – Latviel Jun 08 '22 at 11:50

1 Answers1

0

I found a solution or better the problem. I always test the first point by using the debug function. And always stop at the line after

out_file << str;

But to this position, for whatever reason (maybe someone can explain!?), only a part of the file was written. So I had never run my code to the end. Now I have run it through and it works. Thanks anyway for your time.

Latviel
  • 1
  • 3