Function to read doubles...

I'm trying to write a function that is passed a pointer to a string * filename and a double[] data array. The function is supose to read in a set of double data from the text file and store it in the array.

The data in file is of format


1.3245
1.3456 
..
.


The following is the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void ReadInFiles(string * filename, double[] * dataArray)
{
	ifstream myFile;
	double num;
	myFile.open(*filename);
	if(!myFile)
	{
		cerr << "Unable to open file datafile.txt";
		exit(1);   // call system to stop
	}

	int i = 0;
	while(!myFile.eof)
	{ 
		num = myFile.get();
		*dataArray[i] = num;
		i++;
	}
}


My questions are

1) I know I'm passing the parameters wrong. Can someone please tell me the correct way to write it?

2) Is the method for reading in double array correct? I'm using <fstream>
Personally if I HAD to return an array I would make it the return value because you don't know what size it is before you read in the data. So I would create the array inside the function.

1
2
3
4
5
6
7
#include <string>

double* ReadInFiles(const std::string& filename)
{
    //... create array
    //... read in values.. resizing array as required
}


Do you *have to* read the values into an array for the assignment? Or do you have a choice?

Because it would be much easier and better to read the values into a std::vector which is like an array but better and safer.
@Galik: Thanks. No I don't have to use arrays. In fact, after your suggestion, I'm trying to use vectors now.

However, my biggest concern is with reading in the data from file. The entries I have in the text file are stored as doubles. Should I read it in as a string first and then cast it to double or I can just directly store it into the vector<double>.

I tried using the following but it doesn't seem to work.

1
2
3
4
5
6
7
8
ifstream newfile;
string line;
newfile.open("example.txt");
while(!newfile.eof())
{ getline(newfile, line);
   cout << line << endl;
}
newfile.close();


P.S. I have already looked at the tutorial n IO in c++ available here.
I wouldn't use getline(). Its best to read the values directly into a double using the >> operator:

1
2
3
4
5

double value;
newfile >> value;
list.push_back(value);  // stuff it in the std::vector
Last edited on
You can avoid using pointers altogether.

1
2
3
4
5
6
7
8
9
10
11
vector <double> ReadInDoubles( const string& filename )
  {
  vector <double> result;
  double          d;
  ifstream        inf( filename.c_str() );
  while (inf >> d)
    {
    result.push_back( d );
    }
  return result;
  }

Hope this helps.
Thank you sooo much Duoas,

I literally spent more than 6 hours trying to figure out how to convert string to double. I tried using strtod but it didn't work.

The good thing is that the above works. However, the bad thing is, I still don't know how to convert a string to double. I tried all the examples from google search but it always had some error related to the 1st character.

Eventually, :( I gave up... for now :).
The trick is to put the string into an istringstream (input string stream).

Then just read it into a double variable like you would from a file stream:

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <sstream> // string stream

std::string my_string = "3.58734"; // string

std::istringstream iss(my_string);

double d;

iss >> d; // string to double :o) 
Last edited on
if you have a std::string and you wish to convert to double
then:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <sstream>
#include <string>


std::stringstream  ss;
std::string str("1.2345");  //string
double  dd;

ss << str;
ss >> dd;

std::cout << dd; 


EDIT: Must learn to type faster
Last edited on
Thanks Galik and guestGulkan.

Can you please explain what are these two lines doing??

1
2
ss << str;
ss >> dd;


I thought string to double was strtod() method? :(. I like the above much better through.
stringstream is an interesting object which you can find out more about here.
http://cplusplus.com/reference/iostream/stringstream/

The first line reads from your string into ss.
http://cplusplus.com/reference/iostream/ostream/operator%3C%3C/

The second line converts what's in ss to a double and passes it to that double.
http://cplusplus.com/reference/iostream/istream/operator%3E%3E/

Operator overloading for the win.

-Albatross

The second line converts what's in ss to a double and passes it to that double.
http://cplusplus.com/reference/iostream/istream/operator%3E%3E/


Thanks a lot. This solves the big mystery.
Topic archived. No new replies allowed.