Very simply reading in data :-) ......then into a vector

Pages: 12
Hmm I'm thinkin you're getting garbage. By default, vectors are assigned a size I believe. Push_back(), adds a new element at the end. So, I think index 0 is never initialized, so you just have garbage in there.
Check if the file was opened successfully? If you failed to read anything from the file your vector will be empty and that's probably why you get a garbage value printed.

Thanks for the help people :-) I have checked and the file does open correctly. I have also tested it with a .txt file containing:

1
2
3
4
5
6


and the code worked perfectly....

This made me realise that I am not specifying the type of value in the vector correctly when it come to the data that I actually want to work with. The initial value that I want to read in is

1369.23

which is clearly not an integer. However when I change

vector<int>

to

vector<float>

Instead of getting 1369.23 as I want, I get

1.31466e-38

then the second time that I run the program (having changed nothing in the code!) I get

3.55549e-39

and every time after that it is a different number (seems to be slowly decreasing) at around that order of magnitude (which I believe is about as small as a 'float' can get). I can get the number to remain constant by using

vector<double>

but that keeps giving me a value of 1.81325e-310 which is clearly not the value I am looking for. Thanks again for all your help. How can something so simple prove so trouble some? I'm sure I must be doing something very stupid here :-P
Last edited on
Hmm, might try converting input from String to double before storing it. I can't imagine why that would be causing an issue (since the compiler should be doing some type conversion on its own), but who knows.

I think I might be honing in on the problem here.

OK. So the first value I want to read in from my txt file is 1369.23 . When I set my program to read in the first value in the vector I am getting 1369 (close...... but no cigar!). The second element then is the crazy 1.81325e-310 and everything from then on is messed up. This has lead me to believe that something is going wrong with recognising the decimal point. This is compounded by the fact that when I replace all of the decimals with a 'space' the program seems to work fine. Only this time obviously the second value in the vector is now what should be after the decimal which is still no good. Can anyone see why there might be a problem with using a decimal place in my program? (I am specifying the values as 'double'). Cheers!
Well, decimals in computer systems are not as straight forward as you'd think. Representing floating point numbers in binary is an odd thing and it's no perfect science.

Anyways, can you post your code you're using to read from the file? I think the problem might be there

It's pretty much the same as the last time I posted it. Anyway here it is;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <cerrno>
#include <vector>

using namespace std;

std::vector<double> read_from_file()
{
std::ifstream file("myfile.txt");
std::vector<double> numbers;

int n;
while( file >> n ) numbers.push_back(n) ;

return numbers;
}

int main ()
{
vector<double> printnumbers;
printnumbers = read_from_file();
cout << printnumbers[0] << "\n" ;

return 0;
}


and the first three numbers that I want to read in from 'myfile.txt' are;

1
2
3
1369.23
1297.00
1232.41


Thanks so much for your help it is really appreciated. I never imagined something that at first glance seems like a simple thing to do would prove so troublesome. Cheers! :-)
closed account (DSLq5Di1)
Line 13!

Finally cracked it!

I knew it would be something stupid somewhere.

Thanks so much for all your time and help everyone!!! :-) :-)
Topic archived. No new replies allowed.
Pages: 12