如何使用c ++调整文本文件中代码的缩进?

问题描述 投票:0回答:1

我正在将文件1的代码复制到文件2,但是我希望文件2中的代码看起来像这样用缩进进行调整:在开始缩进= 0时,每个大括号打开都会增加缩进的深度,每个大括号都会关闭例如减少缩进4个空格。我需要帮助来解决此问题

char preCh;
int depth=0;
    int tab = 3;
    int d = 0;
    int pos = 0;
    file1.get(ch);
    while(!file1.eof())
    {
            if(ch=='{')
            {
                d++;
            }
            if(ch=='}'){
                d--;
            }
            depth = tab * d;
            if(preCh == '{' && ch=='\n'){
            file2.put(ch);
                for (int i = 0; i <= depth; i++)
                {
                     file2.put(' ');
                }
            }
                else
                file2.put(ch);
        preCh = ch;
        ch = file1.get();
    }
}

结果必须像代码编辑器一样缩进:

int main(){ 
    if(a>0)
    {
       something();
    }
}
c++ file stream styles indentation
1个回答
0
投票

也许,对您来说出乎意料,对您的问题没有简单的答案。

因此,您的代码将永远无法工作。

© www.soinside.com 2019 - 2024. All rights reserved.