代码之家  ›  专栏  ›  技术社区  ›  apis17

c++提取字符串

  •  2
  • apis17  · 技术社区  · 15 年前

    我有 26/01/10 09:20:20 MAL BIN BIN275 TSTCB U8L5 O/CR ..N UCOS Operated

    我想把36列提取成60列,也就是

    BIN275 TSTCB U8L5 O/CR

    O/CR
    

    有什么简单的解决办法吗?已经做了,但没用。

    #include <iostream>
    #include <string.h>
    #include <fstream>
    using namespace std;
    int main()
    {
    FILE * pFile;
    char mystring [100];
    int string_length;
    
    ofstream output;
    
    pFile = fopen ("input.txt" , "r");
    output.open("output.txt", ios:: out);
    
    
    fgets (mystring , 100 , pFile);
    puts (mystring);
    
    string_length = strlen(mystring);
    
    int i=36;
    
    while (i < 60) 
    {
    output<<mystring[i];
    ++i;
    }
    
    
    fclose (pFile);
    output.close();
    return 0;
    
    }
    

    谢谢您

    2 回复  |  直到 15 年前
        1
  •  2
  •   Amardeep AC9MF    15 年前

    你的程序基本上可以运行,但列号不正确。尝试:

    int i=26;
    
    while (i < 48)
    

    它给了我你指定的结果。

        2
  •  2
  •   Loki Astari    15 年前

    #include <fstream>
    #include <string>
    
    int main()
    {
        int const colLeft  = 36; // or 26
        int const colRight = 60; // or 48
    
        std::ifstream input("input.txt");
        std::ofstream output("output.txt");
    
        std::string  line;
        std::getline(input,line);
    
        output << line.substr(colLeft,(colRight-colLeft)+1);
    }