如何在C++中实现带符号分析的无限循环(不使用C库,处理任意长输入)
Got it, let's tackle this problem step by step. You want to replicate that C-style input handling with 10-byte chunks for symbol analysis, but in C++ without relying on the C standard library. Here's how you can do it:
Core Approach
Instead of using fgets (which is part of the C standard library), we'll use C++'s built-in iostream utilities to read characters one at a time. We'll manually maintain a 10-character buffer, and whenever the buffer fills up, we'll run our symbol analysis on it. After processing, we reset the buffer and keep going—this mimics the chunked processing your original C code did.
Full Implementation Code
#include <iostream> // Replace this with your actual symbol analysis logic void process_chunk(const char* chunk, size_t length) { std::cout << "Analyzing chunk of size " << length << ": "; for (size_t i = 0; i < length; ++i) { // Example analysis: print each character and its ASCII value std::cout << "'" << chunk[i] << "' (" << static_cast<int>(chunk[i]) << ") "; } std::cout << "\n"; } int main() { const size_t BUFFER_SIZE = 10; char buffer[BUFFER_SIZE]; size_t current_pos = 0; // Infinite loop until user sends EOF (Ctrl+D on Unix, Ctrl+Z on Windows) while (true) { char c; // Read a single character (including whitespace/newlines) std::cin.get(c); // Check if we've reached end of input if (std::cin.eof()) { // Process any remaining characters in the buffer if (current_pos > 0) { process_chunk(buffer, current_pos); } break; } // Add the character to our buffer buffer[current_pos++] = c; // If buffer is full, process it and reset if (current_pos == BUFFER_SIZE) { process_chunk(buffer, BUFFER_SIZE); current_pos = 0; } } return 0; }
Key Details Explained
- Reading Characters: We use
std::cin.get(c)because it reads every single character (including spaces, tabs, and newlines) without skipping anything—just likefgetsdoes. This ensures we don't miss any input for our symbol analysis. - Buffer Management: We track
current_posto keep track of how many characters are in the buffer. When it hits 10, we process the chunk and reset the position to start fresh. - Handling EOF: When the user terminates input (via EOF), we don't just exit—we process any leftover characters in the buffer that didn't fill up a full 10-character chunk. That way, we don't lose any input data.
- Symbol Analysis Function: The
process_chunkfunction is a placeholder—you can replace it with your actual logic (checking for specific symbols, character types, etc.). The example just prints each character and its ASCII value to show how the chunk is passed in.
Important Notes
- Error Handling: If you need to handle input errors (like invalid streams), you can add a check for
std::cin.fail()and reset the stream state withstd::cin.clear()if needed. - No C Standard Library: This implementation only uses C++'s
iostream—nostdio.hfunctions likefgets,getchar, etc., so it meets your requirement of avoiding the C standard library.
内容的提问来源于stack exchange,提问作者ioleg19029700
相关产品推荐
相关产品推荐

