gpt4 book ai didi

c++ - 如何用直引号C++替换特殊引号

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:13 24 4
gpt4 key购买 nike

例如,我正在解析的文件包含 unicode char u201d 即。 ”(重音引用)

如何用“(直引号)替换它?

最佳答案

使用 c++ 和 STL 我会使用这样的代码,你仍然需要将输出缓冲区保存到文件.. 在 linux 上测试。

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

// load file data
char* load_file(const char *filename)
{
FILE *fp;

fp = fopen(filename, "r");
if (!fp)
return NULL;

size_t size;
if ((0 != fseek(fp, 0, SEEK_END)) || (-1 == (size = ftell(fp))))
size = 0;

// set fp at file start
fseek(fp, 0, 0);

char *buffer;
buffer = (char*) malloc(size);
if(!buffer)
{
fclose (fp);
return NULL;
}

if(size != fread(buffer, 1, size, fp))
{
free (buffer);
buffer = NULL;
}

fclose (fp);
return buffer;
}

// replace string
std::string replace(const std::string& str, const std::string& from, const std::string& to)
{
if(str.size() < 1)
return str;

std::string temp_str(str);

size_t start_pos = 0;
while((start_pos = temp_str.find(from, start_pos)) != std::string::npos)
{
temp_str.replace(start_pos, from.length(), to);
start_pos += to.length();
}

return temp_str.c_str();
}


int main(int argc, char** argv)
{
const char* file_name = "test.txt";

char* file_bytes = load_file(file_name);

if(file_bytes == nullptr)
return EXIT_FAILURE;

std::cout << replace(file_bytes, "”", "\"") << std::endl;
return EXIT_SUCCESS;
}

关于c++ - 如何用直引号C++替换特殊引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46558127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com