C ++如何替换代码中的异常引号 [英] C++ How to replace unusual quotes in code

查看:94
本文介绍了C ++如何替换代码中的异常引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当您从文档中复制代码时,它会获得行号和奇怪的引号。我已经编写了一个脚本来删除这些初始数字,但是很难找到一种方法来删除那些奇怪的引号’,因此,我已经包含了完整的代码。它读入文件,然后输出格式化的文件。但是编译器警告说这些引号是多个字符,我猜这意味着非标准的ASCII字符。有点用,但这不是一个很好的解决方案。任何帮助表示赞赏:

sometimes when you copy code from a document it gets line numbers and strange quotes. I've written a script to remove those initial numbers but it is very hard to find a way to remove those strange quotes ‘’"" so I've included my full code. It reads in a file and puts out a formatted file. But the compiler warns that these quotes are multi characters, which I guess means non standard ascii chars. It kinda works but it's not a great solution. Any help appreciated:

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

using namespace std;

string replaceChar(string str, char ch1, char ch2);

// Main
int main(int argc, char *argv[]) {

    string line;

    fstream stri, stro;
    // ifstream in
    stri.open(argv[1], ios::in);
    if(stri.fail()){
        cerr << "File failed to open for input" << endl;
        return 1;
    }

    // ofstream out
    stro.open("file_out.txt", ios::out);
    if(stro.fail()){
        cerr << "File failed to open for output" << endl;
        return 1;
    }

    // Read - Write
    //stri.get(c);
    getline(stri, line, '\n');
    while(!stri.eof()){
        // Remove numbers
        line.erase(0,3);

        //line.replace( line.begin(), line.end(), "‘", "\'" );
        //line.replace( line.begin(), line.end(), "’", "\'" );
        //line.replace( line.begin(), line.end(), """, "\'" );
        //line.replace( line.begin(), line.end(), """, "\'" );
        line = replaceChar(line, '‘','\'');
        line = replaceChar(line, '’','\'');
        line = replaceChar(line, '"','\"');
        line = replaceChar(line, '"','\"');

        stro << line << endl;
        getline(stri, line, '\n');
    }

    // Close files
    stri.close();
    stro.close();

    // Output
    cout << "File Edited Ok!";
    //cout << count -1 << " characters copied."<< endl; 
}

string replaceChar(string str, char ch1, char ch2) {
  for (int i = 0; i < str.length(); ++i) {
    if (str[i] == ch1)
      str[i] = ch2;
  }

  return str;
}


推荐答案

好吧,不是漂亮,但有效。任何人都想完善搜索那些该死的奇怪引号之一作为我的客人!

Ok, it ain't pretty, but it works. Anyone want to refine searching for one of those damned strange quote marks be my guest!

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

using namespace std;

// Function Declaration
bool replace(string& str, const string& from, const string& to);

bool checkMyLine(string line);

// Main
int main(int argc, char *argv[]) {

    // line to edit
    string line;

    fstream stri, stro;
    // ifstream in
    stri.open(argv[1], ios::in);
    if(stri.fail()){
        cerr << "File failed to open for input" << endl;
        return 1;
    }

    // ofstream out
    stro.open("file_out.txt", ios::out);
    if(stro.fail()){
        cerr << "File failed to open for output" << endl;
        return 1;
    }

    // Read - Write
    while(getline(stri, line, '\n')){

        // Remove numbers at start of each line followed by space, eg: "001: "
    int i;
    for(i = 0;i < line.length();i++)
    {
        if(line[i] == ' ') break;
    }
    line.erase(0,i+1);

        //Replace Odd Chars
        for(int i=0;i<line.length();i++)
        {
        replace(line, "\u2018","\u0027");   // replaces ‘
        replace(line, "\u2019","\u0027");   // replaces ’
        replace(line, "\u201C","\u0022");   // replaces "
        replace(line, "\u201D","\u0022");   // replaces "
        }

        // Write to file
        stro << line << endl;
    }

    // Close files
    stri.close();
    stro.close();

    // Output Message
    cout << "File Edited Ok!";
}// End of Main
//
bool replace(string& str, const string& from, const string& to) 
{
    size_t start_pos = str.find(from);
    if(start_pos == string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}

这篇关于C ++如何替换代码中的异常引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆