zoukankan      html  css  js  c++  java
  • C++读取中文或英文文件空格分割

    // show file content - sbumpc() example
    #include <iostream>     // std::cout, std::streambuf
    #include <fstream>      // std::ifstream
    #include <cstdio>       // EOF
    
    using namespace std;
    
    
    bool readWord(std::istream& in, std::string& word) {
        int c;
        std::streambuf& sb = *in.rdbuf();
        word.clear();
        cout << "---------------1";
    
        while ((c = sb.sbumpc()) != EOF) {
            if (c == ' ' || c == '
    ' || c == '
    ' || c == '	' || c == 'v' ||
                c == 'f' || c == '') {
                
                if (word.empty()) {
                    if (c == '
    ') {
                        cout << "---------------2";
                        word += "END";
                        return true;
                    }
                    continue;
                } else {
                    if (c == '
    ')
                        sb.sungetc(); // advance to next position
                        cout << "---------------5";
                    return true;
                }
    
            }
            cout << "---------------3";
            word.push_back(c);
        }
        // trigger eofbit
        in.get();
        cout << "---------------4" << word.empty();
        return !word.empty();
    }
    
    int main () {
      
      std::ifstream fin ("test.txt");
      std::string word;
      while (readWord(fin, word)) {
        cout << word << endl;
      }
      
      
      return 0;
    }

    test.txt

    asdf 你好 中国 万里 长城
    asaa asa as asa
    vdvsd
    sjklmn
    sdfsdf

    输出:

  • 相关阅读:
    P4281 [AHOI2008]紧急集合 / 聚会
    P2622 关灯问题II
    CF1092F Tree with Maximum Cost
    10.28记
    威尔逊定理及证明
    CF895C Square Subsets
    洛谷 P5556 圣剑护符
    Multi-nim结论及证明
    AT2667 [AGC017D] Game on Tree
    洛谷 P4643 [国家集训队]阿狸和桃子的游戏
  • 原文地址:https://www.cnblogs.com/TMatrix52/p/11304536.html
Copyright © 2011-2022 走看看