DEV Community

海前 王
海前 王

Posted on

get




/**#include <iostream>
#include <fstream>

int main() {
    std::ifstream file("1.txt");
    char buffer[100];

    if (file) {
        // Read a chunk of data into buffer
        file.get(buffer, sizeof(buffer),'1');

        // Output the number of characters actually read
        std::streamsize numChars = file.gcount();
        std::cout << "Number of characters read: " << numChars << std::endl;

        // Null-terminate the string and output it
        buffer[numChars] = '\0';
        std::cout << "Content read: " << buffer << std::endl;
    }

    return 0;
}

#include <iostream>
#include <sstream>

int main() {
    // 创建一个包含文本的字符串流
    std::istringstream input("Hello, world! This is a test.");

    // 创建一个 stringbuf 对象来存储读取的内容
    std::stringbuf sb;

    // 从 input 流中读取字符,直到遇到字符 '!'
    input.get(sb, '!');

    // 获取读取的字符串
    std::string result = sb.str();

    // 输出结果
    std::cout << "Read content: " << result << std::endl;

    //this cannot change the string 
    std::cout<<"\n"<<input.str();

    return 0;
}
 */
#include <iostream>

int main() {
    char ch;
    while ((ch = std::cin.peek()) != '.' && ch != '\n') {
        std::cin.get(ch);
        std::cout << ch; // 打印读取的字符
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)