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] << ' ';
}
c++ vector fstream
add a comment |
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] << ' ';
}
c++ vector fstream
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
add a comment |
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] << ' ';
}
c++ vector fstream
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
c++ vector fstream
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
add a comment |
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
add a comment |
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);
}
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
add a comment |
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);
}
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
add a comment |
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);
}
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
add a comment |
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);
}
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);
}
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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