Reading data with scientific notation from txtx file

Jun 1, 2012 at 9:31pm
I am trying to get data to an array from a text file. But the data is in scientific notation. So my program reads wrong!
So can anybody please show me the way....
my coding is:
int main()
{
double b[19][6];
ifstream myfile1("data_store.txt");
if(myfile1.is_open())
{
while(!myfile1.eof())
{
for(int i=0;i<19;i++)
{
for(int j=0;j<6;j++)
{
myfile1>>b[i][j];
}
}
}
myfile1.close();
}

else
{
cout<<"error.....";
}

for(int i=0;i<19;i++)
{
for(int j=0;j<6;j++)
{
cout<<b[i][j];
//cout<<"\t";
}
cout<<"\n";
}
return 0;
}

Jun 1, 2012 at 9:40pm
Hey Pubudu, using the code tags (the <> icon) really helps others when reading your code.

How is your program reading wrong?

Will you show us an example of what you're supplying as input and what your program provides as output?
Jun 2, 2012 at 4:12pm
Thanks sexyBachelor....my input file data_store.txt is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-5.0E+03  2.1E+03  3.0E+02  2.8E+03  -1.6E+02  2.2E+03
-4.9E+03  2.2E+03  2.9E+03  1.9E+03  -1.8E+02  2.1E+03
-2.8E+03 -1.6E+02  2.2E+03  5.1E+03  -2.3E+02  2.4E+03
-4.2E+03 
-4.1E+03
-4.5E+03
-4.9E+03
-4.8E+03
-4.7E+03
-4.1E+03
-3.9E+03
-3.8E+03
-3.9E+03
-4.2E+03



The txt file has 19 rows and 6 columns.....my program code 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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	double b[19][6];
	ifstream myfile1("data_store.txt");
	if(myfile1.is_open())
	{
		while(!myfile1.eof())
		{
			for(int i=0;i<19;i++)
			{
				for(int j=0;j<6;j++)
				{
					myfile1>>b[i][j];
				}
			}
		}
		myfile1.close();
	}
	else
	{
		cout<<"error.....";
	}

	for(int i=0;i<19;i++)
	{
		for(int j=0;j<6;j++)
		{
			cout<<b[i][j];
			//cout<<"\t";
		}
		cout<<"\n";
	}
	return 0;
}


The out shows the error message "error....." although the data_store.txt and the program is in the same folder.
Jun 2, 2012 at 7:23pm
You've really explained your problem much better.

From what you've said the error is because the file is not open.

What kind of compiler/ide are you using? Is it an IDE or a command-line compiler?

I'm guessing you are using an IDE.

If so, go to your bin/debug or bin/release folder (whichever you are building.. debug or release) and put your text file in there. Then run the program.

The only other problem I could think of is that you may not be running the program with the correct permissions. Make sure you have full permissions on your operating system if this fails.
Jun 2, 2012 at 8:22pm
okay....it worked!!!
I am using MS visual c++ express on winxp.

Thanks SexyBachelor.........
Jun 3, 2012 at 1:11am
Line 9 shouldn't even run because you aren't opening a file or anything. You say it works, but that's probably because of Visual Studio's ability to fix some syntax errors on its own. It should have given you a warning.
Jun 3, 2012 at 11:15pm
Actually, you may not be aware of this, but ifstream allows the programmer to pass in the name of the file to be opened in the constructor. ifstream will automatically attempt to open the file passed.

In other words,
1
2
//version 1
ifstream stream = ifstream("filename.extension");


and
1
2
3
//version 2
ifstream stream;
stream.open("filename.extension");


do the same exact thing.
Jun 3, 2012 at 11:18pm
At risk of beating it to death, here are the constructors for an ifstream; as can be seen, it happily accepts a filename to open (and since C++11, will accept it as a proper C++ string).

http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream
Jun 4, 2012 at 12:12am
What do you know. You do learn something new each day.
Topic archived. No new replies allowed.