c++ 표준 입력 관련 질문이 있습니다 (cin)

bao의 이미지

cin으로 string 객체에 문자열을 받으려고 하는데 최대 4096개 정도 밖에 안들어가는데...리눅스 콘솔 화면에서 입력하고 있습니다...해당 내용을
파일에 적고
input.txt | ./test

이런식으로 값을 입력해도 다 못 받고 있습니다...

이 이상 받으려면 어떻게 해야 하나요?

shint의 이미지

- cin 의 rdbuf() 로. stringstream 에 값을 전달 하고 출력 : 성공
- cin 입력 안되고 넘어감.

http://goorm.io

#include <iostream>
 
 
#include <string>
#include <sstream>
 
 
using namespace std;
 
 
 
int main()
{
	system("clear");
 
	string a;
	a.resize(10000);
a = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789aaaaa";
	cout << "a.max_size()" << a.max_size() << endl;
 
	//
	stringstream ss;
 
	// Prepare fake stringstream
	std::stringstream fake_cin;
 
	//ss << cin.rdbuf();
	//   fake_cin << cin.rdbuf();
	fake_cin << a;
 
	// Backup and change std::cin's streambuffer
	std::streambuf *backup = std::cin.rdbuf(); // back up cin's streambuf
	std::streambuf *psbuf = fake_cin.rdbuf(); // get file's streambuf
	std::cin.rdbuf(psbuf); // assign streambuf to cin
 
	// Read something, will come from out stringstream
	std::string input;
	std::cin >> input;
 
	// Verify that we actually read the right text
	std::cout << input << std::endl;
 
	// Restore old situation
	std::cin.rdbuf(backup); // restore cin's original streambuf	
 
	//
	cout << ss.str() << endl;
 
	return 0;
}

[C++] 입력버퍼에서 데이터를 넣어놓는 함수 있나요?
https://www.codentalks.com/t/topic/842

setvbuf(3) - Linux man page
https://linux.die.net/man/3/setvbuf

std::cin
http://www.cplusplus.com/reference/iostream/cin/

C++ ignore 입력 버퍼 비우기
http://blog.naver.com/PostView.nhn?blogId=lyw94k&logNo=220859905348

STRING(3) Linux Programmer's Manual STRING(3)
http://man7.org/linux/man-pages/man3/string.3.html

std::string
http://www.cplusplus.com/reference/string/string/

std::string::max_size
http://www.cplusplus.com/reference/string/string/max_size/

std::string::resize
http://www.cplusplus.com/reference/string/string/resize/

Reading getline from cin into a stringstream (C++)
https://stackoverflow.com/questions/35974402/reading-getline-from-cin-into-a-stringstream-c

cin >> stringstream?
https://www.daniweb.com/programming/software-development/threads/456327/cin-stringstream

sstream << cin블라블라 에서 막혀서 질문드립니다.
https://www.codentalks.com/t/topic/45/22

#ifdef _MSC_VER
#pragma warning(disable: 4996)
#include <io.h>
#else
#include <unistd.h>
#endif
 
template< int       BUFFER_SIZE     = 0x10000 >
class               COSE_INPUT
{
    unsigned char   inputs[ BUFFER_SIZE ];
    unsigned char*  ptr;
    unsigned char*  limit;
 
public:
    void            load()
    {
        ptr = inputs;
        size_t size = read( 0, inputs, BUFFER_SIZE - 10 );
        unsigned char* end = inputs + size;
        while( end[ -1 ] >= '0' && size )
            end += size = read( 0, end, 1 );
        limit = end - 2 * ( end != ptr );
    }
 
    int             get_int()
    {
    	if( ptr > limit )
            load();
        while( *ptr < '0' ) ptr++;
    	int value;
        for( value = 0; *ptr >= '0'; ++ptr )
            value = value * 10 + ( *ptr & 0xF );
        return  value;
    }
 
    void            put( size_t n )
    {
        static unsigned char temp[ 12 ] =
            { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '\n', 0 };
        unsigned char* p = temp + ( sizeof temp - 3 );
        do *p-- = n % 10 | '0', n /= 10; while( n );
        write( 1, p + 1, temp - p + ( sizeof temp - 2 ) );
    }
};
 
사용법
 
int main()
{
    COSE_INPUT<> coin;
    coin.load();
    const int N = coin.get_int();
    // to do
    return  0;
}

cin to stringstream
https://bytes.com/topic/c/answers/132443-cin-stringstream

std::stringstream
http://www.cplusplus.com/reference/sstream/stringstream/

stringstream
http://www.cplusplus.com/forum/beginner/3952/

How to temporarily redirect std::cin from a std::stringstream
https://www.smork.info/blog/2010/01/26/entry100126-131539.html

#include <iostream>
#include <sstream>
 
int main()
{
   // Prepare fake stringstream
   std::stringstream fake_cin;
   fake_cin << "Hi";
 
   // Backup and change std::cin's streambuffer
   std::streambuf *backup = std::cin.rdbuf(); // back up cin's streambuf
   std::streambuf *psbuf = fake_cin.rdbuf(); // get file's streambuf
   std::cin.rdbuf(psbuf); // assign streambuf to cin
 
   // Read something, will come from out stringstream
   std::string input;
   std::cin >> input;
 
   // Verify that we actually read the right text
   std::cout << input << std::endl;
 
   // Restore old situation
   std::cin.rdbuf(backup); // restore cin's original streambuf
}

C++ Notes: String stream example
http://www.fredosaurus.com/notes-cpp/strings/stringstream-example.html

stringstream in C++ and its applications
https://www.geeksforgeeks.org/stringstream-c-applications/

C++ Stringstreams
https://latedev.wordpress.com/2011/11/16/c-stringstreams/

cin and cout are different
http://www.cplusplus.com/forum/beginner/14200/

Basic Input/Output
http://www.cplusplus.com/doc/tutorial/basic_io/

cin >> stringstream?
https://www.daniweb.com/programming/software-development/threads/456327/cin-stringstream

Mystr << cin.rdbuf();

c++ execute function any time a stream is written to
https://stackoverflow.com/questions/12826751/c-execute-function-any-time-a-stream-is-written-to

std::basic_stringstream
https://en.cppreference.com/w/cpp/io/basic_stringstream

std::basic_ios::rdbuf
https://en.cppreference.com/w/cpp/io/basic_ios/rdbuf

std::ios::rdbuf
http://www.cplusplus.com/reference/ios/ios/rdbuf/

How to read until EOF from cin in C++
https://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c

std::istreambuf_iterator<char> begin(std::cin), end;
std::string s(begin, end);
std::cout << s;

The C++ Standard Library: A Tutorial and Reference
https://books.google.co.kr/books?id=9DEJKhasp7gC&pg=PA847&lpg=PA847&dq=cin.rdbuf+EOF&source=bl&ots=_bZdP4EwWB&sig=ccJZOsY2q-mNTUOQCLTSJvWdh_E&hl=ko&sa=X&ved=2ahUKEwj8jbK-mLLdAhXB4IMKHQeSBp8Q6AEwA3oECBEQAQ#v=onepage&q=cin.rdbuf%20EOF&f=false

std::cin >> std::noskipws >> std::cout.rdbuf();

get int from stdin
http://rextester.com/XEDB66063

Going Beyond End of File:
More Filtering Streambufs
http://gabisoft.free.fr/articles/fltrsbf2.html

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

익명 사용자의 이미지

그런 문제가 발생할 일반적인 이유는 없습니다.
코드에 뭔가 문제가 있는 모양인데, 직접 살펴보지 않고는 알 수가 없겠네요.

bao의 이미지

setvbuf (stdin, NULL, _IOFBF, 16384); 함수 이용해서 버퍼 사이즈를 늘렸습니다! 답변 감사합니다!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.