C++, reading doubles from txt into vector, problems with reading the whole file












-1















So i have a text file with 2 columns of doubles with a tab in between, and try to read them into 2 vectors. My first Problem ist that it does not go through the whole file but starts in the last third. My second Problem is that while it does push_back it converts the numbers in some other numbers.. i just cant get my head around it.. if i try to just put them all into one string vector it works without problems, but i need them as doubles or int for further processing



ifstream myfile("TextFile",ios::in);

if (!myfile)
{
cout << "Can't oppen" << endl;
system("pause");
return -1;
}



vector<long double> Datenx;
vector<long double> Dateny;
vector<string>lel;

string line;

while (getline(myfile, line)) {

// lel.push_back(line);

string numberx = line.substr(0, 12);
int pos = line.find(" ");
string numbery = line.substr(pos + 1, 12);
stringstream iss(numberx);

long double x = 0.0;

iss>> setprecision(10)>>fixed >>showpoint >> x;
//cout <<fixed<< numberx << endl;
//cout<<setprecision(10)<<fixed<< x << endl;

Datenx.push_back(x);
stringstream is(numbery);
long double y = 0.0;
is >> y;
Dateny.push_back(y);
}
for (int n = 0; n < 100; n++) {
cout << Datenx[n] << ' ' << endl;
}
// cout << fixed << Datenx[2] << ' ' << endl;

cin.get();
return 0;


Part of input file:



0.0000000000  0.0006536954

0.0000000100 0.0005515555

0.0000000200 0.0005004856

0.0000000300 0.0001327819

0.0000000400 0.0006945514

0.0000000500 0.0007864773

0.0000000600 0.0001327819

0.0000000700 0.0007354074


Output: Datenx vector:



0

1e-08

2e-08

3e-08


...



Output: Dateny vector:



0.000653695

0.000551555

0.000500486

0.000132782


so the Dateny is kinda right.. it cuts the last digit
and the Datenx vector is total wrong..










share|improve this question









New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

    – BoBTFish
    11 hours ago













  • Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

    – Alan Birtles
    11 hours ago






  • 1





    You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

    – Some programmer dude
    11 hours ago













  • And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

    – Some programmer dude
    11 hours ago













  • Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

    – Some programmer dude
    11 hours ago
















-1















So i have a text file with 2 columns of doubles with a tab in between, and try to read them into 2 vectors. My first Problem ist that it does not go through the whole file but starts in the last third. My second Problem is that while it does push_back it converts the numbers in some other numbers.. i just cant get my head around it.. if i try to just put them all into one string vector it works without problems, but i need them as doubles or int for further processing



ifstream myfile("TextFile",ios::in);

if (!myfile)
{
cout << "Can't oppen" << endl;
system("pause");
return -1;
}



vector<long double> Datenx;
vector<long double> Dateny;
vector<string>lel;

string line;

while (getline(myfile, line)) {

// lel.push_back(line);

string numberx = line.substr(0, 12);
int pos = line.find(" ");
string numbery = line.substr(pos + 1, 12);
stringstream iss(numberx);

long double x = 0.0;

iss>> setprecision(10)>>fixed >>showpoint >> x;
//cout <<fixed<< numberx << endl;
//cout<<setprecision(10)<<fixed<< x << endl;

Datenx.push_back(x);
stringstream is(numbery);
long double y = 0.0;
is >> y;
Dateny.push_back(y);
}
for (int n = 0; n < 100; n++) {
cout << Datenx[n] << ' ' << endl;
}
// cout << fixed << Datenx[2] << ' ' << endl;

cin.get();
return 0;


Part of input file:



0.0000000000  0.0006536954

0.0000000100 0.0005515555

0.0000000200 0.0005004856

0.0000000300 0.0001327819

0.0000000400 0.0006945514

0.0000000500 0.0007864773

0.0000000600 0.0001327819

0.0000000700 0.0007354074


Output: Datenx vector:



0

1e-08

2e-08

3e-08


...



Output: Dateny vector:



0.000653695

0.000551555

0.000500486

0.000132782


so the Dateny is kinda right.. it cuts the last digit
and the Datenx vector is total wrong..










share|improve this question









New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

    – BoBTFish
    11 hours ago













  • Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

    – Alan Birtles
    11 hours ago






  • 1





    You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

    – Some programmer dude
    11 hours ago













  • And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

    – Some programmer dude
    11 hours ago













  • Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

    – Some programmer dude
    11 hours ago














-1












-1








-1








So i have a text file with 2 columns of doubles with a tab in between, and try to read them into 2 vectors. My first Problem ist that it does not go through the whole file but starts in the last third. My second Problem is that while it does push_back it converts the numbers in some other numbers.. i just cant get my head around it.. if i try to just put them all into one string vector it works without problems, but i need them as doubles or int for further processing



ifstream myfile("TextFile",ios::in);

if (!myfile)
{
cout << "Can't oppen" << endl;
system("pause");
return -1;
}



vector<long double> Datenx;
vector<long double> Dateny;
vector<string>lel;

string line;

while (getline(myfile, line)) {

// lel.push_back(line);

string numberx = line.substr(0, 12);
int pos = line.find(" ");
string numbery = line.substr(pos + 1, 12);
stringstream iss(numberx);

long double x = 0.0;

iss>> setprecision(10)>>fixed >>showpoint >> x;
//cout <<fixed<< numberx << endl;
//cout<<setprecision(10)<<fixed<< x << endl;

Datenx.push_back(x);
stringstream is(numbery);
long double y = 0.0;
is >> y;
Dateny.push_back(y);
}
for (int n = 0; n < 100; n++) {
cout << Datenx[n] << ' ' << endl;
}
// cout << fixed << Datenx[2] << ' ' << endl;

cin.get();
return 0;


Part of input file:



0.0000000000  0.0006536954

0.0000000100 0.0005515555

0.0000000200 0.0005004856

0.0000000300 0.0001327819

0.0000000400 0.0006945514

0.0000000500 0.0007864773

0.0000000600 0.0001327819

0.0000000700 0.0007354074


Output: Datenx vector:



0

1e-08

2e-08

3e-08


...



Output: Dateny vector:



0.000653695

0.000551555

0.000500486

0.000132782


so the Dateny is kinda right.. it cuts the last digit
and the Datenx vector is total wrong..










share|improve this question









New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












So i have a text file with 2 columns of doubles with a tab in between, and try to read them into 2 vectors. My first Problem ist that it does not go through the whole file but starts in the last third. My second Problem is that while it does push_back it converts the numbers in some other numbers.. i just cant get my head around it.. if i try to just put them all into one string vector it works without problems, but i need them as doubles or int for further processing



ifstream myfile("TextFile",ios::in);

if (!myfile)
{
cout << "Can't oppen" << endl;
system("pause");
return -1;
}



vector<long double> Datenx;
vector<long double> Dateny;
vector<string>lel;

string line;

while (getline(myfile, line)) {

// lel.push_back(line);

string numberx = line.substr(0, 12);
int pos = line.find(" ");
string numbery = line.substr(pos + 1, 12);
stringstream iss(numberx);

long double x = 0.0;

iss>> setprecision(10)>>fixed >>showpoint >> x;
//cout <<fixed<< numberx << endl;
//cout<<setprecision(10)<<fixed<< x << endl;

Datenx.push_back(x);
stringstream is(numbery);
long double y = 0.0;
is >> y;
Dateny.push_back(y);
}
for (int n = 0; n < 100; n++) {
cout << Datenx[n] << ' ' << endl;
}
// cout << fixed << Datenx[2] << ' ' << endl;

cin.get();
return 0;


Part of input file:



0.0000000000  0.0006536954

0.0000000100 0.0005515555

0.0000000200 0.0005004856

0.0000000300 0.0001327819

0.0000000400 0.0006945514

0.0000000500 0.0007864773

0.0000000600 0.0001327819

0.0000000700 0.0007354074


Output: Datenx vector:



0

1e-08

2e-08

3e-08


...



Output: Dateny vector:



0.000653695

0.000551555

0.000500486

0.000132782


so the Dateny is kinda right.. it cuts the last digit
and the Datenx vector is total wrong..







c++ vector






share|improve this question









New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 hours ago









BoBTFish

14.7k23558




14.7k23558






New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 11 hours ago









LordHelsingLordHelsing

82




82




New contributor




LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






LordHelsing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

    – BoBTFish
    11 hours ago













  • Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

    – Alan Birtles
    11 hours ago






  • 1





    You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

    – Some programmer dude
    11 hours ago













  • And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

    – Some programmer dude
    11 hours ago













  • Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

    – Some programmer dude
    11 hours ago














  • 1





    Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

    – BoBTFish
    11 hours ago













  • Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

    – Alan Birtles
    11 hours ago






  • 1





    You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

    – Some programmer dude
    11 hours ago













  • And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

    – Some programmer dude
    11 hours ago













  • Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

    – Some programmer dude
    11 hours ago








1




1





Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

– BoBTFish
11 hours ago







Can you provide a small example input file, with the output you get from it, and the output you expect? Have you tried printing out the lines you read in your while loop, and the doubles you parse from that? Right now, this is actually pretty good for a first-time asker, you include most of the code and a pretty good description of the problem, but you don't quite include everything necessary for us to reproduce it.

– BoBTFish
11 hours ago















Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

– Alan Birtles
11 hours ago





Please provide a Minimal, Complete, and Verifiable example, what is your input, what is the expected output, what is the actual output

– Alan Birtles
11 hours ago




1




1





You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

– Some programmer dude
11 hours ago







You know about std::istringstream. I recommend that you use it to parse the line you read with std::getline (instead of your substring method). Like e.g. std::istringstream iss(line); iss >> x >> y;

– Some programmer dude
11 hours ago















And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

– Some programmer dude
11 hours ago







And considering that the x and y values are related, I suggest you use a structure with those as members, and use a vector of that structure instead of two different vectors.

– Some programmer dude
11 hours ago















Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

– Some programmer dude
11 hours ago





Lastly, why do you hard-code 100 for the loop outputting Datenx? Are you sure there are at least 100 elements in the vector?

– Some programmer dude
11 hours ago












1 Answer
1






active

oldest

votes


















2














Try to keep it simple first. If it works, you can add functionality.



#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

int main()
{
std::vector<double> v1, v2;
std::string line;

std::ifstream myFile("input.txt");
while(getline(myFile, line))
{
std::istringstream lineStream(line);
double first, second;
lineStream >> first >> second;
v1.push_back(first);
v2.push_back(second);
}
}


I tried this, with the following "input.txt"



1.1    1.2
2.1 2.2
3.1 3.2
4.1 4.2
5.1 5.2





share|improve this answer


























  • I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

    – BoBTFish
    11 hours ago













  • Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

    – Tom Mekken
    11 hours ago













  • Why use strings in the first place? Why not double first, second; and skip std::stod?

    – Some programmer dude
    10 hours ago











  • @Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

    – Tom Mekken
    10 hours ago











  • That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

    – Some programmer dude
    10 hours ago











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
});


}
});






LordHelsing is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54249558%2fc-reading-doubles-from-txt-into-vector-problems-with-reading-the-whole-file%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














Try to keep it simple first. If it works, you can add functionality.



#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

int main()
{
std::vector<double> v1, v2;
std::string line;

std::ifstream myFile("input.txt");
while(getline(myFile, line))
{
std::istringstream lineStream(line);
double first, second;
lineStream >> first >> second;
v1.push_back(first);
v2.push_back(second);
}
}


I tried this, with the following "input.txt"



1.1    1.2
2.1 2.2
3.1 3.2
4.1 4.2
5.1 5.2





share|improve this answer


























  • I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

    – BoBTFish
    11 hours ago













  • Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

    – Tom Mekken
    11 hours ago













  • Why use strings in the first place? Why not double first, second; and skip std::stod?

    – Some programmer dude
    10 hours ago











  • @Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

    – Tom Mekken
    10 hours ago











  • That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

    – Some programmer dude
    10 hours ago
















2














Try to keep it simple first. If it works, you can add functionality.



#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

int main()
{
std::vector<double> v1, v2;
std::string line;

std::ifstream myFile("input.txt");
while(getline(myFile, line))
{
std::istringstream lineStream(line);
double first, second;
lineStream >> first >> second;
v1.push_back(first);
v2.push_back(second);
}
}


I tried this, with the following "input.txt"



1.1    1.2
2.1 2.2
3.1 3.2
4.1 4.2
5.1 5.2





share|improve this answer


























  • I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

    – BoBTFish
    11 hours ago













  • Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

    – Tom Mekken
    11 hours ago













  • Why use strings in the first place? Why not double first, second; and skip std::stod?

    – Some programmer dude
    10 hours ago











  • @Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

    – Tom Mekken
    10 hours ago











  • That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

    – Some programmer dude
    10 hours ago














2












2








2







Try to keep it simple first. If it works, you can add functionality.



#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

int main()
{
std::vector<double> v1, v2;
std::string line;

std::ifstream myFile("input.txt");
while(getline(myFile, line))
{
std::istringstream lineStream(line);
double first, second;
lineStream >> first >> second;
v1.push_back(first);
v2.push_back(second);
}
}


I tried this, with the following "input.txt"



1.1    1.2
2.1 2.2
3.1 3.2
4.1 4.2
5.1 5.2





share|improve this answer















Try to keep it simple first. If it works, you can add functionality.



#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

int main()
{
std::vector<double> v1, v2;
std::string line;

std::ifstream myFile("input.txt");
while(getline(myFile, line))
{
std::istringstream lineStream(line);
double first, second;
lineStream >> first >> second;
v1.push_back(first);
v2.push_back(second);
}
}


I tried this, with the following "input.txt"



1.1    1.2
2.1 2.2
3.1 3.2
4.1 4.2
5.1 5.2






share|improve this answer














share|improve this answer



share|improve this answer








edited 10 hours ago

























answered 11 hours ago









Tom MekkenTom Mekken

547619




547619













  • I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

    – BoBTFish
    11 hours ago













  • Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

    – Tom Mekken
    11 hours ago













  • Why use strings in the first place? Why not double first, second; and skip std::stod?

    – Some programmer dude
    10 hours ago











  • @Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

    – Tom Mekken
    10 hours ago











  • That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

    – Some programmer dude
    10 hours ago



















  • I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

    – BoBTFish
    11 hours ago













  • Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

    – Tom Mekken
    11 hours ago













  • Why use strings in the first place? Why not double first, second; and skip std::stod?

    – Some programmer dude
    10 hours ago











  • @Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

    – Tom Mekken
    10 hours ago











  • That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

    – Some programmer dude
    10 hours ago

















I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

– BoBTFish
11 hours ago







I'd suggest using std::stod instead of atof, but other than that I like this approach. No need to close() the file either - you might like to be explicit in the code, but I'd argue that using the RAII behaviour of fstream (i.e. the file is closed in the destructor) is safer and more idiomatic.

– BoBTFish
11 hours ago















Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

– Tom Mekken
11 hours ago







Thanks for your suggestion ... I removed the myFile.close() from my answer and changed std::atof to std::stod

– Tom Mekken
11 hours ago















Why use strings in the first place? Why not double first, second; and skip std::stod?

– Some programmer dude
10 hours ago





Why use strings in the first place? Why not double first, second; and skip std::stod?

– Some programmer dude
10 hours ago













@Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

– Tom Mekken
10 hours ago





@Some programmer dude: because I wasn't aware, that this was possible with istringstream ... thank you for your hint :) (every day I learn something new :) )

– Tom Mekken
10 hours ago













That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

– Some programmer dude
10 hours ago





That's the nice thing about streams in C++: They all work the same. It doesn't matter if it's an input string stream or an input file stream, or the private stream behind std::cin. As long as it's derived from std::istream they all work the same.

– Some programmer dude
10 hours ago










LordHelsing is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















LordHelsing is a new contributor. Be nice, and check out our Code of Conduct.













LordHelsing is a new contributor. Be nice, and check out our Code of Conduct.












LordHelsing is a new contributor. Be nice, and check out our Code of Conduct.
















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%2f54249558%2fc-reading-doubles-from-txt-into-vector-problems-with-reading-the-whole-file%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

Callistus III

Plistias Cous

Index Sanctorum