Please forgive me for my naive English and ignorance as I am new to programming and C++.
Trying my best to provide basic information:
- I use CLion (student version) to code in C++.
- In my Windows 10 notebook it seems to be named CLion 2019.3.5 x64.
- It is recently updated to Version 2020.1.
- Installed MinGW (I believe latest, but I'm not sure, sorry).
My question:
Well... As in title, I tried to use std::getline()
to read some text.
Not std::cin.getline()
, but std::getline()
(sorry that I don't quite know the difference, but I found them to be different functions requiring different parameters).
I use std::getline()
because (obviously) I want to read a string with spaces between.
However, it seems that it only reads first 509 characters in my input...
But WHY???????
It may seem more likely if it reads first 512 = 2^9 or something.
Example:
It is just one of the code I tested.
#include <iostream>
int main() {
std::ios_base::sync_with_stdio(false); //THIS IS SOMETHING REMARKABLE in the context of my problem, which I would explain below
std::string testing;
std::getline(std::cin, testing); //There is no '\n' at the beginning so it would work well.
std::cout << "testing.max_size() = " << testing.max_size() << '\n' //Just testing, should be 2147483647 or something, which therefore it's not the problem with how many characters a string can store.
<< "testing.length() = " << testing.length() << '\n'
<< R"(testing = ")" << testing << '"' << std::endl;
return 0;
}
Input:
(Some long string with length 689, actually for my own purpose)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 60 29 61 31 62 33 63 35 64 37 65 39 66 41 67 43 68 45 69 47 70 49 71 51 72 53 73 55 74 19 75 29 76 7 77 31 78 15 79 33 80 7 81 35 82 11 83 37 84 19 85 39 86 15 87 41 88 11 89 43 90 27 91 45 92 25 93 47 94 23 95 49 96 21 97 51 98 3 99 53 100 3 101 55 102 17 103 13 104 27 105 25 106 23 107 21 108 9 109 5 110 13 111 17 112 5 113 9 114 0 115 0 116 0 117 0 118 0 119 0 120 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Output:
testing.max_size() = 2147483647
testing.length() = 509
testing = "60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 9
6 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 60 29 61 31 62 33 63 35 6
4 37 65 39 66 41 67 43 68 45 69 47 70 49 71 51 72 53 73 55 74 19 75 29 76 7 77 31 78 15 79 33 80 7 81 35 82 11 83 37 84
19 85 39 86 15 87 41 88 11 89 43 90 27 91 45 92 25 93 47 94 23 95 49 96 21 97 51 98 3 99 53 100 3 101 55 102 17 103 13 1
04 27 105 25 106 23 107 21 108 9 109 5 1"
(In CLion console, oftenly some lines are broken to two, which I don't know why, but doesn't matter in the context of my question)
Explanation:
As you can see, testing.length() == 509
, and I have tested in C++14 as well as C++20, this project (of CLion) as well as other project opened, and of course with other texts (at position 509 there is no special characters, which I tried by replacing it with other characters but still get the same result).
And by the way I have the habit of using std::ios_base::sync_with_stdio(false)
to increase the speed of input and output, which I think it's nice to me (familiar with this line of code in the beginning of my program).
However, (may save your testing time), the problem is with std::ios_base::sync_with_stdio(false)
. When I remove this line, the program works very well, even for strings a few times longer in length.
However, I don't like omitting this line, as I think it does increase the speed of i/o and it is not necessary to remove it in general.
Also, it is very specific to CLion (as far as I know about C++ and programming).
I tested (with std::ios_base::sync_with_stdio(false)
) in some online C++ compiler and it works fine (testing.length() == 689
for my input):
Summarization of my problem:
Why, exclusively in CLion (but not some online compilers), the code std::ios_base::sync_with_stdio(false)
exclusively makes std::getline()
read at most exactly 509 characters (I tested on several projects)?
This doesn't seem reasonable to me...
More:
I tried to search for similar problems, but sorry, I can't find any (very) relevant posts.
I would like some detailed but comprehensive answers, if possible, as I am not an expert in C++ (though I am willing to learn some more about C++).
I hope I can solve this problem without stop using CLion / stop using std::ios_base::sync_with_stdio(false)
(competitive programming!) / stop using std::getline()
, thx.
- Yes I know the perfomance increase is normally small, see answer to this post, but most of the time
std::ios_base::sync_with_stdio(false)
is important to me, so is there some fix or whatsoever that don't requirestd::ios_base::sync_with_stdio(true)
? Even if no, I would wonder why...
Any help would be appreciated, thank you!