How to convert streamsize to other or how to pass streamsize value to vector?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a problem i need to pass streamsize value to vector. if i'm not pass it to fixed size. it will overhead with emplace_back with thousand element. the error compiler is possible cause lost data.



ifstream input(i_inputFilePath, ios::binary);
if (input.is_open())
{
ofstream output(o_outputFilePath, ios::binary);
std::array<char, 1024> buffer;
while (true) {
input.read(buffer.data(), buffer.size());
streamsize dataSize = input.gcount();
if (dataSize)
{
std::vector<char> data(dataSize); // here the problem
for (DWORD i = 0; i < dataSize; i++)
{
data.emplace_back(buffer[i]);
}
std::rotate(data.begin(), data.begin() + 1, data.end());
output.write(data.data(), dataSize);
}
else
{
output.close();
input.close();
break;
}
}
}
else
{
cout << "File is not exist";
}









share|improve this question

























  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

    – Some programmer dude
    Nov 24 '18 at 14:58











  • @Someprogrammerdude its already full. i edited my question with comment to point to problem.

    – stephen.hawk
    Nov 24 '18 at 15:01


















0















I have a problem i need to pass streamsize value to vector. if i'm not pass it to fixed size. it will overhead with emplace_back with thousand element. the error compiler is possible cause lost data.



ifstream input(i_inputFilePath, ios::binary);
if (input.is_open())
{
ofstream output(o_outputFilePath, ios::binary);
std::array<char, 1024> buffer;
while (true) {
input.read(buffer.data(), buffer.size());
streamsize dataSize = input.gcount();
if (dataSize)
{
std::vector<char> data(dataSize); // here the problem
for (DWORD i = 0; i < dataSize; i++)
{
data.emplace_back(buffer[i]);
}
std::rotate(data.begin(), data.begin() + 1, data.end());
output.write(data.data(), dataSize);
}
else
{
output.close();
input.close();
break;
}
}
}
else
{
cout << "File is not exist";
}









share|improve this question

























  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

    – Some programmer dude
    Nov 24 '18 at 14:58











  • @Someprogrammerdude its already full. i edited my question with comment to point to problem.

    – stephen.hawk
    Nov 24 '18 at 15:01














0












0








0








I have a problem i need to pass streamsize value to vector. if i'm not pass it to fixed size. it will overhead with emplace_back with thousand element. the error compiler is possible cause lost data.



ifstream input(i_inputFilePath, ios::binary);
if (input.is_open())
{
ofstream output(o_outputFilePath, ios::binary);
std::array<char, 1024> buffer;
while (true) {
input.read(buffer.data(), buffer.size());
streamsize dataSize = input.gcount();
if (dataSize)
{
std::vector<char> data(dataSize); // here the problem
for (DWORD i = 0; i < dataSize; i++)
{
data.emplace_back(buffer[i]);
}
std::rotate(data.begin(), data.begin() + 1, data.end());
output.write(data.data(), dataSize);
}
else
{
output.close();
input.close();
break;
}
}
}
else
{
cout << "File is not exist";
}









share|improve this question
















I have a problem i need to pass streamsize value to vector. if i'm not pass it to fixed size. it will overhead with emplace_back with thousand element. the error compiler is possible cause lost data.



ifstream input(i_inputFilePath, ios::binary);
if (input.is_open())
{
ofstream output(o_outputFilePath, ios::binary);
std::array<char, 1024> buffer;
while (true) {
input.read(buffer.data(), buffer.size());
streamsize dataSize = input.gcount();
if (dataSize)
{
std::vector<char> data(dataSize); // here the problem
for (DWORD i = 0; i < dataSize; i++)
{
data.emplace_back(buffer[i]);
}
std::rotate(data.begin(), data.begin() + 1, data.end());
output.write(data.data(), dataSize);
}
else
{
output.close();
input.close();
break;
}
}
}
else
{
cout << "File is not exist";
}






c++ windows visual-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 15:00







stephen.hawk

















asked Nov 24 '18 at 14:52









stephen.hawkstephen.hawk

12




12













  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

    – Some programmer dude
    Nov 24 '18 at 14:58











  • @Someprogrammerdude its already full. i edited my question with comment to point to problem.

    – stephen.hawk
    Nov 24 '18 at 15:01



















  • Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

    – Some programmer dude
    Nov 24 '18 at 14:58











  • @Someprogrammerdude its already full. i edited my question with comment to point to problem.

    – stephen.hawk
    Nov 24 '18 at 15:01

















Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

– Some programmer dude
Nov 24 '18 at 14:58





Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Also learn how to create a Minimal, Complete, and Verifiable example. And if asking about build error, please include the copy-pasted full and complete output, and add a comment in the code to show where it happens.

– Some programmer dude
Nov 24 '18 at 14:58













@Someprogrammerdude its already full. i edited my question with comment to point to problem.

– stephen.hawk
Nov 24 '18 at 15:01





@Someprogrammerdude its already full. i edited my question with comment to point to problem.

– stephen.hawk
Nov 24 '18 at 15:01












1 Answer
1






active

oldest

votes


















2














If you pass a single integer argument to the constructor of std::vector (as in std::vector<char> data(dataSize);) is will default-construct that many elements in it and its size will be the integer passed.



If you then emplace_back into it, it will increase its size, not overwrite the already constructed elements.



If you want to emplace_back all your elements, simply do so, no need to pass the final size to the constructor:



std::vector<char> data;


Most implementations of std::vector will not reallocate on each emplace_back, but will e.g. double the allocated space each time it runs out of space. Therefore this will not really be that bad on average.



If you are concerned about reallocations, then you can force the vector to pre-allocate space with



data.reserve(dataSize);


This will not actually construct any elements but make sure the necessary space is allocated in advance.



If you are getting an error message about the conversion from std::streamsize to std::size_t, then this is a false-positive, because in this particular case you know that dataSize can only have values ranging from 0 to 1024. The compiler is correct in warning that dataSize's type is signed and could therefore be holding a negative value that can not be represented by std::size_t, which is unsigned.



You can test whether this assumption is correct with something like



streamsize dataSize = input.gcount();
if(dataSize < 0 || dataSize > buffer.size()) {
throw std::range_error("dataSize has unexpected value!");
}
data.reserve(static_cast<std::size_t>(dataSize));


This makes sure that dataSize is not accidentally negative or larger than expected (if it is, it will throw an exception) and otherwise it will hint at the compiler that you really want to do the narrowing conversion with the explicit cast, which should suppress the warning/error message.






share|improve this answer


























  • i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

    – stephen.hawk
    Nov 24 '18 at 15:03











  • the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

    – stephen.hawk
    Nov 24 '18 at 15:11











  • i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

    – stephen.hawk
    Nov 24 '18 at 15:21











  • i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

    – stephen.hawk
    Nov 24 '18 at 15:23











  • my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

    – stephen.hawk
    Nov 24 '18 at 15:29












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459369%2fhow-to-convert-streamsize-to-other-or-how-to-pass-streamsize-value-to-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you pass a single integer argument to the constructor of std::vector (as in std::vector<char> data(dataSize);) is will default-construct that many elements in it and its size will be the integer passed.



If you then emplace_back into it, it will increase its size, not overwrite the already constructed elements.



If you want to emplace_back all your elements, simply do so, no need to pass the final size to the constructor:



std::vector<char> data;


Most implementations of std::vector will not reallocate on each emplace_back, but will e.g. double the allocated space each time it runs out of space. Therefore this will not really be that bad on average.



If you are concerned about reallocations, then you can force the vector to pre-allocate space with



data.reserve(dataSize);


This will not actually construct any elements but make sure the necessary space is allocated in advance.



If you are getting an error message about the conversion from std::streamsize to std::size_t, then this is a false-positive, because in this particular case you know that dataSize can only have values ranging from 0 to 1024. The compiler is correct in warning that dataSize's type is signed and could therefore be holding a negative value that can not be represented by std::size_t, which is unsigned.



You can test whether this assumption is correct with something like



streamsize dataSize = input.gcount();
if(dataSize < 0 || dataSize > buffer.size()) {
throw std::range_error("dataSize has unexpected value!");
}
data.reserve(static_cast<std::size_t>(dataSize));


This makes sure that dataSize is not accidentally negative or larger than expected (if it is, it will throw an exception) and otherwise it will hint at the compiler that you really want to do the narrowing conversion with the explicit cast, which should suppress the warning/error message.






share|improve this answer


























  • i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

    – stephen.hawk
    Nov 24 '18 at 15:03











  • the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

    – stephen.hawk
    Nov 24 '18 at 15:11











  • i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

    – stephen.hawk
    Nov 24 '18 at 15:21











  • i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

    – stephen.hawk
    Nov 24 '18 at 15:23











  • my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

    – stephen.hawk
    Nov 24 '18 at 15:29
















2














If you pass a single integer argument to the constructor of std::vector (as in std::vector<char> data(dataSize);) is will default-construct that many elements in it and its size will be the integer passed.



If you then emplace_back into it, it will increase its size, not overwrite the already constructed elements.



If you want to emplace_back all your elements, simply do so, no need to pass the final size to the constructor:



std::vector<char> data;


Most implementations of std::vector will not reallocate on each emplace_back, but will e.g. double the allocated space each time it runs out of space. Therefore this will not really be that bad on average.



If you are concerned about reallocations, then you can force the vector to pre-allocate space with



data.reserve(dataSize);


This will not actually construct any elements but make sure the necessary space is allocated in advance.



If you are getting an error message about the conversion from std::streamsize to std::size_t, then this is a false-positive, because in this particular case you know that dataSize can only have values ranging from 0 to 1024. The compiler is correct in warning that dataSize's type is signed and could therefore be holding a negative value that can not be represented by std::size_t, which is unsigned.



You can test whether this assumption is correct with something like



streamsize dataSize = input.gcount();
if(dataSize < 0 || dataSize > buffer.size()) {
throw std::range_error("dataSize has unexpected value!");
}
data.reserve(static_cast<std::size_t>(dataSize));


This makes sure that dataSize is not accidentally negative or larger than expected (if it is, it will throw an exception) and otherwise it will hint at the compiler that you really want to do the narrowing conversion with the explicit cast, which should suppress the warning/error message.






share|improve this answer


























  • i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

    – stephen.hawk
    Nov 24 '18 at 15:03











  • the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

    – stephen.hawk
    Nov 24 '18 at 15:11











  • i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

    – stephen.hawk
    Nov 24 '18 at 15:21











  • i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

    – stephen.hawk
    Nov 24 '18 at 15:23











  • my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

    – stephen.hawk
    Nov 24 '18 at 15:29














2












2








2







If you pass a single integer argument to the constructor of std::vector (as in std::vector<char> data(dataSize);) is will default-construct that many elements in it and its size will be the integer passed.



If you then emplace_back into it, it will increase its size, not overwrite the already constructed elements.



If you want to emplace_back all your elements, simply do so, no need to pass the final size to the constructor:



std::vector<char> data;


Most implementations of std::vector will not reallocate on each emplace_back, but will e.g. double the allocated space each time it runs out of space. Therefore this will not really be that bad on average.



If you are concerned about reallocations, then you can force the vector to pre-allocate space with



data.reserve(dataSize);


This will not actually construct any elements but make sure the necessary space is allocated in advance.



If you are getting an error message about the conversion from std::streamsize to std::size_t, then this is a false-positive, because in this particular case you know that dataSize can only have values ranging from 0 to 1024. The compiler is correct in warning that dataSize's type is signed and could therefore be holding a negative value that can not be represented by std::size_t, which is unsigned.



You can test whether this assumption is correct with something like



streamsize dataSize = input.gcount();
if(dataSize < 0 || dataSize > buffer.size()) {
throw std::range_error("dataSize has unexpected value!");
}
data.reserve(static_cast<std::size_t>(dataSize));


This makes sure that dataSize is not accidentally negative or larger than expected (if it is, it will throw an exception) and otherwise it will hint at the compiler that you really want to do the narrowing conversion with the explicit cast, which should suppress the warning/error message.






share|improve this answer















If you pass a single integer argument to the constructor of std::vector (as in std::vector<char> data(dataSize);) is will default-construct that many elements in it and its size will be the integer passed.



If you then emplace_back into it, it will increase its size, not overwrite the already constructed elements.



If you want to emplace_back all your elements, simply do so, no need to pass the final size to the constructor:



std::vector<char> data;


Most implementations of std::vector will not reallocate on each emplace_back, but will e.g. double the allocated space each time it runs out of space. Therefore this will not really be that bad on average.



If you are concerned about reallocations, then you can force the vector to pre-allocate space with



data.reserve(dataSize);


This will not actually construct any elements but make sure the necessary space is allocated in advance.



If you are getting an error message about the conversion from std::streamsize to std::size_t, then this is a false-positive, because in this particular case you know that dataSize can only have values ranging from 0 to 1024. The compiler is correct in warning that dataSize's type is signed and could therefore be holding a negative value that can not be represented by std::size_t, which is unsigned.



You can test whether this assumption is correct with something like



streamsize dataSize = input.gcount();
if(dataSize < 0 || dataSize > buffer.size()) {
throw std::range_error("dataSize has unexpected value!");
}
data.reserve(static_cast<std::size_t>(dataSize));


This makes sure that dataSize is not accidentally negative or larger than expected (if it is, it will throw an exception) and otherwise it will hint at the compiler that you really want to do the narrowing conversion with the explicit cast, which should suppress the warning/error message.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 16:29

























answered Nov 24 '18 at 15:01









user10605163user10605163

2,868624




2,868624













  • i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

    – stephen.hawk
    Nov 24 '18 at 15:03











  • the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

    – stephen.hawk
    Nov 24 '18 at 15:11











  • i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

    – stephen.hawk
    Nov 24 '18 at 15:21











  • i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

    – stephen.hawk
    Nov 24 '18 at 15:23











  • my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

    – stephen.hawk
    Nov 24 '18 at 15:29



















  • i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

    – stephen.hawk
    Nov 24 '18 at 15:03











  • the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

    – stephen.hawk
    Nov 24 '18 at 15:11











  • i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

    – stephen.hawk
    Nov 24 '18 at 15:21











  • i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

    – stephen.hawk
    Nov 24 '18 at 15:23











  • my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

    – stephen.hawk
    Nov 24 '18 at 15:29

















i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

– stephen.hawk
Nov 24 '18 at 15:03





i know but it keep resize. it will make overhead if 1 million element keep resize by 1 to fit. why the vector need to keep resize if i know the buffer size?

– stephen.hawk
Nov 24 '18 at 15:03













the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

– stephen.hawk
Nov 24 '18 at 15:11





the problem the streamsize dataSize don't want to allow convert. it same with data.reserve(dataSize)

– stephen.hawk
Nov 24 '18 at 15:11













i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

– stephen.hawk
Nov 24 '18 at 15:21





i use visual studio 2017 community with language c++17. unicode code. and MT Build. is there something wrong iwth my config?

– stephen.hawk
Nov 24 '18 at 15:21













i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

– stephen.hawk
Nov 24 '18 at 15:23





i check it to in my compile streamsize is typedef long long. but when i crreate long long variabel and initiliziation with streamsize. sampe result can't convert possible lose data.

– stephen.hawk
Nov 24 '18 at 15:23













my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

– stephen.hawk
Nov 24 '18 at 15:29





my currently work code is i rework itu not using vector instead manual dynamic allocate with new. but i need to implement rotate by ownself

– stephen.hawk
Nov 24 '18 at 15:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459369%2fhow-to-convert-streamsize-to-other-or-how-to-pass-streamsize-value-to-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini