Problem with reading a std::vector from a txt file (reads only one number)











up vote
-1
down vote

favorite












I have a problem that the part of the internet that I went through can't solve.
It's my very first encounter with fstream and no matter what I do the program won't import more than one integer from the text file. I'd love if anyone with knowledge could help me a little. Here is the code that I'm using at the moment.
Writing:



void writeVector(const std::vector<int>& vec, const std::string& fileName) {
std::ofstream save;
save.open(fileName);
if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << &vec[i];
}

std::cout << "Plik zapisano pomyslnie.n";
}
save.close();
}


Reading:



std::vector<int> readVector(const std::string& fileName) {
std::ifstream read(fileName);
std::vector<int> buffer;
if (read) {
int value;
while (read >> value) {
buffer.push_back(value);
}
}
return buffer;
}

void zad4() {
std::string fileName = "Zadanie 2";
print(readVector(fileName));
}


Plus what's bothering me is the code in the txt file since it is something like: (00000248EC48FE5000000248EC48FE5400000248EC48FE5800000248EC48FE5C etc).
When I print "buffer" it only gives me one number. I have made sure that the vector is correctly initialized. What can be the problem?




Short answer: (credit: Sam Varshavchik)




if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << vec[i] << ' ';
}









share|improve this question




















  • 1




    A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
    – Sam Varshavchik
    Nov 9 at 17:31












  • The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
    – user4581301
    Nov 9 at 17:50










  • @SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
    – 2137Pablo
    Nov 9 at 19:12















up vote
-1
down vote

favorite












I have a problem that the part of the internet that I went through can't solve.
It's my very first encounter with fstream and no matter what I do the program won't import more than one integer from the text file. I'd love if anyone with knowledge could help me a little. Here is the code that I'm using at the moment.
Writing:



void writeVector(const std::vector<int>& vec, const std::string& fileName) {
std::ofstream save;
save.open(fileName);
if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << &vec[i];
}

std::cout << "Plik zapisano pomyslnie.n";
}
save.close();
}


Reading:



std::vector<int> readVector(const std::string& fileName) {
std::ifstream read(fileName);
std::vector<int> buffer;
if (read) {
int value;
while (read >> value) {
buffer.push_back(value);
}
}
return buffer;
}

void zad4() {
std::string fileName = "Zadanie 2";
print(readVector(fileName));
}


Plus what's bothering me is the code in the txt file since it is something like: (00000248EC48FE5000000248EC48FE5400000248EC48FE5800000248EC48FE5C etc).
When I print "buffer" it only gives me one number. I have made sure that the vector is correctly initialized. What can be the problem?




Short answer: (credit: Sam Varshavchik)




if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << vec[i] << ' ';
}









share|improve this question




















  • 1




    A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
    – Sam Varshavchik
    Nov 9 at 17:31












  • The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
    – user4581301
    Nov 9 at 17:50










  • @SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
    – 2137Pablo
    Nov 9 at 19:12













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a problem that the part of the internet that I went through can't solve.
It's my very first encounter with fstream and no matter what I do the program won't import more than one integer from the text file. I'd love if anyone with knowledge could help me a little. Here is the code that I'm using at the moment.
Writing:



void writeVector(const std::vector<int>& vec, const std::string& fileName) {
std::ofstream save;
save.open(fileName);
if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << &vec[i];
}

std::cout << "Plik zapisano pomyslnie.n";
}
save.close();
}


Reading:



std::vector<int> readVector(const std::string& fileName) {
std::ifstream read(fileName);
std::vector<int> buffer;
if (read) {
int value;
while (read >> value) {
buffer.push_back(value);
}
}
return buffer;
}

void zad4() {
std::string fileName = "Zadanie 2";
print(readVector(fileName));
}


Plus what's bothering me is the code in the txt file since it is something like: (00000248EC48FE5000000248EC48FE5400000248EC48FE5800000248EC48FE5C etc).
When I print "buffer" it only gives me one number. I have made sure that the vector is correctly initialized. What can be the problem?




Short answer: (credit: Sam Varshavchik)




if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << vec[i] << ' ';
}









share|improve this question















I have a problem that the part of the internet that I went through can't solve.
It's my very first encounter with fstream and no matter what I do the program won't import more than one integer from the text file. I'd love if anyone with knowledge could help me a little. Here is the code that I'm using at the moment.
Writing:



void writeVector(const std::vector<int>& vec, const std::string& fileName) {
std::ofstream save;
save.open(fileName);
if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << &vec[i];
}

std::cout << "Plik zapisano pomyslnie.n";
}
save.close();
}


Reading:



std::vector<int> readVector(const std::string& fileName) {
std::ifstream read(fileName);
std::vector<int> buffer;
if (read) {
int value;
while (read >> value) {
buffer.push_back(value);
}
}
return buffer;
}

void zad4() {
std::string fileName = "Zadanie 2";
print(readVector(fileName));
}


Plus what's bothering me is the code in the txt file since it is something like: (00000248EC48FE5000000248EC48FE5400000248EC48FE5800000248EC48FE5C etc).
When I print "buffer" it only gives me one number. I have made sure that the vector is correctly initialized. What can be the problem?




Short answer: (credit: Sam Varshavchik)




if (save) {
for (int i = 0; i < vec.size(); ++i) {
save << vec[i] << ' ';
}






c++ vector fstream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:20

























asked Nov 9 at 17:29









2137Pablo

12




12








  • 1




    A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
    – Sam Varshavchik
    Nov 9 at 17:31












  • The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
    – user4581301
    Nov 9 at 17:50










  • @SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
    – 2137Pablo
    Nov 9 at 19:12














  • 1




    A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
    – Sam Varshavchik
    Nov 9 at 17:31












  • The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
    – user4581301
    Nov 9 at 17:50










  • @SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
    – 2137Pablo
    Nov 9 at 19:12








1




1




A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
– Sam Varshavchik
Nov 9 at 17:31






A computer will always do exactly what you tell your computer to do, and not what you want it to do. save << &vec[i]; -- you just told your computer to write an address of this integer to the file. If you look at the file, you will see a bunch of hexadecimal garbage in it, instead of your integer values. In addition to that, another thing you never told your computer to do is to add spaces between all values in your output file.
– Sam Varshavchik
Nov 9 at 17:31














The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
– user4581301
Nov 9 at 17:50




The spaces are really important. Without some way to tell one number from the next, they might as well all be one number. Eg if you use << to write the numbers one through four to a file and don't specify a delimiter the file will contain 1234, which >> is going to read back as a single number.
– user4581301
Nov 9 at 17:50












@SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
– 2137Pablo
Nov 9 at 19:12




@SamVarshavchik thank you for your time, that was exactly the case. The topic of this homework were references and I completely forgot that this way I'm going to get the address as the value.
– 2137Pablo
Nov 9 at 19:12












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The data in the file looks like a string of hexadecimal values. Your attempt to read this data tries to read it using decimal. As it starts of with a decimal digit and the sequence of decimal digits don't exceed the maximal value for int the read attempt manages to get one value. That is almost certainly not what you'd actually want to do with this data.



Now, what to actually do with this data is a separate question. You may want to read suitable groups of hex characters into corresponding integers. Whether that is the correct thing to do depends on what the data is supposed to mean. There seems to be a repeating pattern in the section you quoted (rewritten with the string broke after 16 character/8 bytes):



00000248EC48FE50
00000248EC48FE54
00000248EC48FE58
00000248EC48FE5C


That seems to imply that the data contains some sort of 64 bit data spelled in hex. The formatted input used by IOStreams really likes to get spaces. One way to decode this data is to read it into a buffer with 16 characters, update a string stream with this data, and read data from there as hex data:



std::istringstream sin;
sin >> std::hex;
char buffer[16];
while (read.read(buffer, 16)) {
sin.str(std::string(buffer, buffer + read.gcount()));
sin.clear();
std::uint64_t value;
if (!(sin >> value)) {
break;
}
buffer.push_back(value);
}





share|improve this answer





















  • Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
    – 2137Pablo
    Nov 9 at 19:09













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',
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%2f53230640%2fproblem-with-reading-a-stdvector-from-a-txt-file-reads-only-one-number%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








up vote
0
down vote













The data in the file looks like a string of hexadecimal values. Your attempt to read this data tries to read it using decimal. As it starts of with a decimal digit and the sequence of decimal digits don't exceed the maximal value for int the read attempt manages to get one value. That is almost certainly not what you'd actually want to do with this data.



Now, what to actually do with this data is a separate question. You may want to read suitable groups of hex characters into corresponding integers. Whether that is the correct thing to do depends on what the data is supposed to mean. There seems to be a repeating pattern in the section you quoted (rewritten with the string broke after 16 character/8 bytes):



00000248EC48FE50
00000248EC48FE54
00000248EC48FE58
00000248EC48FE5C


That seems to imply that the data contains some sort of 64 bit data spelled in hex. The formatted input used by IOStreams really likes to get spaces. One way to decode this data is to read it into a buffer with 16 characters, update a string stream with this data, and read data from there as hex data:



std::istringstream sin;
sin >> std::hex;
char buffer[16];
while (read.read(buffer, 16)) {
sin.str(std::string(buffer, buffer + read.gcount()));
sin.clear();
std::uint64_t value;
if (!(sin >> value)) {
break;
}
buffer.push_back(value);
}





share|improve this answer





















  • Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
    – 2137Pablo
    Nov 9 at 19:09

















up vote
0
down vote













The data in the file looks like a string of hexadecimal values. Your attempt to read this data tries to read it using decimal. As it starts of with a decimal digit and the sequence of decimal digits don't exceed the maximal value for int the read attempt manages to get one value. That is almost certainly not what you'd actually want to do with this data.



Now, what to actually do with this data is a separate question. You may want to read suitable groups of hex characters into corresponding integers. Whether that is the correct thing to do depends on what the data is supposed to mean. There seems to be a repeating pattern in the section you quoted (rewritten with the string broke after 16 character/8 bytes):



00000248EC48FE50
00000248EC48FE54
00000248EC48FE58
00000248EC48FE5C


That seems to imply that the data contains some sort of 64 bit data spelled in hex. The formatted input used by IOStreams really likes to get spaces. One way to decode this data is to read it into a buffer with 16 characters, update a string stream with this data, and read data from there as hex data:



std::istringstream sin;
sin >> std::hex;
char buffer[16];
while (read.read(buffer, 16)) {
sin.str(std::string(buffer, buffer + read.gcount()));
sin.clear();
std::uint64_t value;
if (!(sin >> value)) {
break;
}
buffer.push_back(value);
}





share|improve this answer





















  • Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
    – 2137Pablo
    Nov 9 at 19:09















up vote
0
down vote










up vote
0
down vote









The data in the file looks like a string of hexadecimal values. Your attempt to read this data tries to read it using decimal. As it starts of with a decimal digit and the sequence of decimal digits don't exceed the maximal value for int the read attempt manages to get one value. That is almost certainly not what you'd actually want to do with this data.



Now, what to actually do with this data is a separate question. You may want to read suitable groups of hex characters into corresponding integers. Whether that is the correct thing to do depends on what the data is supposed to mean. There seems to be a repeating pattern in the section you quoted (rewritten with the string broke after 16 character/8 bytes):



00000248EC48FE50
00000248EC48FE54
00000248EC48FE58
00000248EC48FE5C


That seems to imply that the data contains some sort of 64 bit data spelled in hex. The formatted input used by IOStreams really likes to get spaces. One way to decode this data is to read it into a buffer with 16 characters, update a string stream with this data, and read data from there as hex data:



std::istringstream sin;
sin >> std::hex;
char buffer[16];
while (read.read(buffer, 16)) {
sin.str(std::string(buffer, buffer + read.gcount()));
sin.clear();
std::uint64_t value;
if (!(sin >> value)) {
break;
}
buffer.push_back(value);
}





share|improve this answer












The data in the file looks like a string of hexadecimal values. Your attempt to read this data tries to read it using decimal. As it starts of with a decimal digit and the sequence of decimal digits don't exceed the maximal value for int the read attempt manages to get one value. That is almost certainly not what you'd actually want to do with this data.



Now, what to actually do with this data is a separate question. You may want to read suitable groups of hex characters into corresponding integers. Whether that is the correct thing to do depends on what the data is supposed to mean. There seems to be a repeating pattern in the section you quoted (rewritten with the string broke after 16 character/8 bytes):



00000248EC48FE50
00000248EC48FE54
00000248EC48FE58
00000248EC48FE5C


That seems to imply that the data contains some sort of 64 bit data spelled in hex. The formatted input used by IOStreams really likes to get spaces. One way to decode this data is to read it into a buffer with 16 characters, update a string stream with this data, and read data from there as hex data:



std::istringstream sin;
sin >> std::hex;
char buffer[16];
while (read.read(buffer, 16)) {
sin.str(std::string(buffer, buffer + read.gcount()));
sin.clear();
std::uint64_t value;
if (!(sin >> value)) {
break;
}
buffer.push_back(value);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 18:07









Dietmar Kühl

124k9150313




124k9150313












  • Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
    – 2137Pablo
    Nov 9 at 19:09




















  • Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
    – 2137Pablo
    Nov 9 at 19:09


















Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
– 2137Pablo
Nov 9 at 19:09






Thank you very much for your reply, the problem was that I exported the adress when I wanted just the numbers. This mistake must've been because this works topic was references and I thought that I'm reffering to the values. Thank you for your time!
– 2137Pablo
Nov 9 at 19:09




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230640%2fproblem-with-reading-a-stdvector-from-a-txt-file-reads-only-one-number%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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)