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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay