C++ Rewrite a file but leaving out everything before a word

前端 未结 5 397
天涯浪人
天涯浪人 2020-12-17 01:25

I\'m using Visual C++ Express 2010... and I\'m very new to C++.

I want to read a file then remove everything before the word \"<--START-->\" and rewrite the file

5条回答
  •  萌比男神i
    2020-12-17 02:13

    Here's a version that doesn't have to read the whole file into memory. Note: uses C stdio, not iostreams (just because I know C stdio a lot better); reads from standard input, writes to standard output.

    #include 
    
    int
    main(void)
    {
        int c;
        enum { BOF, LT, D1, D2, S, T1, A, R, T2, D3, D4, GO } state = BOF;
        while ((c = getchar()) != EOF)
            switch (state)
            {
            case BOF: state = c == '<' ? LT : BOF; break;
            case LT:  state = c == '-' ? D1 : c == '<' ? LT : BOF; break;
            case D1:  state = c == '-' ? D2 : c == '<' ? LT : BOF; break;
            case D2:  state = c == 'S' ? S  : c == '<' ? LT : BOF; break;
            case S:   state = c == 'T' ? T1 : c == '<' ? LT : BOF; break;
            case T1:  state = c == 'A' ? A  : c == '<' ? LT : BOF; break;
            case A:   state = c == 'R' ? R  : c == '<' ? LT : BOF; break;
            case R:   state = c == 'T' ? T2 : c == '<' ? LT : BOF; break;
            case T2:  state = c == '-' ? D3 : c == '<' ? LT : BOF; break;
            case D3:  state = c == '-' ? D4 : c == '<' ? LT : BOF; break;
            case D4:  state = c == '>' ? GO : c == '<' ? LT : BOF; break;
            case GO:  putchar(c); break;
            }
        return 0;
    }
    

提交回复
热议问题