C++去除数字和符号前缀

自动去除文件中以数字和’.'开头的前缀

  • 例如:
    输入:123.#include
    输出:#include

主要通过读取文件中的字符然后输出到一个文件,需要提前建立好这两个文件,可以看到代码中的两个文件路径,修改为自己的
代码:

/*
Author:wikl
Description:删除文本中数字开头和数字加‘.’开头的部分
*/
#include
using namespace std;
string handle(string s){
    int n=s.size();
    if(n==0)
        return s;
    int flag=0;
    for(int i=0;i<n;i++){
        if(s[i]>'9'||s[i]<'0'&&s[i]!='.'){
            flag=i;
            break;
        }
    }
    s=s.substr(flag);
    return s;
}
int main(){
    ifstream myfile("D:\\VScodeC++\\file\\hello.txt",ios::in); 
    string temp; 
    if (!myfile.is_open()) 
    { 
        cout << "未成功打开文件" << endl; 
    }
    string res;
    while(getline(myfile,temp)){
        res+=handle(temp);
        res+='\n';
    }

    fstream f;
    f.open("D:\\VScodeC++\\file\\text.txt", ios::out);
    if (!f.is_open()) 
    { 
        cout << "未成功打开文件" << endl; 
    }
    f<<res<<endl;
    cout<<res;
    cout<<"已写入文件text中!"<<endl;
    myfile.close(); 
    f.close();
    
    system("start D:\\VScodeC++\\file\\text.txt");//自动打开文件
    system("pause");
    return 0; 
}


实例:
hello.txt

235.#include <stdio.h>
3.int main(){
6.printf("hello world");
7.printf(56);
8.return 0;
}

text.txt为空

运行结果:
C++去除数字和符号前缀_第1张图片

你可能感兴趣的:(数据结构,C++,c++,字符串)