在流(C++)中编辑文件(HTML)
我正在为新项目寻求一些帮助,并且在C ++编程中绝对是新的。
我有一个HTML模板(尺寸〜8KB),里面有一些占位符。我的任务是阅读此模板编辑占位符,并根据我的来源添加一些或多或少的容器。
我尝试了不同的方式,但总是被卡在另一个点上。
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);
}
}
替换占位符的替换工作正常,但是整个流的一部分(HTML文件)只能存储在“ out_file”中。其余的迷路了。我大约要大约8kb的文件大小,但在我看来这不是问题。
输出是“完整的”,因此将写入整个流。但是,取代占位符会覆盖我不想的某些部分。占位符于新价值较小,其余标志在占位符占据占位符之后的零件之后。
。
我优先于第一个变体,因为我更容易用新的输入代替占位符。但是我希望您能帮助我理解我的第一个(对我来说)的第一个,也许是错误的步骤。 谢谢你的宝贵时间。我很感激。我准备学习...
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);
}
}
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.
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案或更好的问题。我总是使用调试功能测试第一点。并且始终在该职位上停下来
,但出于何种原因(也许有人可以解释!?),只写了一部分文件。因此,我从未将代码运行到最后。现在我已经完成了,它可以工作。
无论如何,感谢您的时间。
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
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.