468,484 Members | 1,584 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

help please with c++ file i/o

hi, I am having trouble with file input to the array I made in the
program.

1. I want to measure how much data are there in the file, and

2. put all the datas into array. (number of array is same as the number
of columns of data)

the file looks somewhat like this.

0.00000000E+0 -5.00000000E+1 3.33823774E-15
1.00000000E+0 -4.99000015E+1 3.45138779E-15
2.00000000E+0 -4.97999992E+1 3.56837278E-15
3.00000000E+0 -4.97000008E+1 3.68932315E-15
4.00000000E+0 -4.95999985E+1 3.81437317E-15
5.00000000E+0 -4.95000000E+1 3.94366216E-15
6.00000000E+0 -4.94000015E+1 4.07733285E-15
7.00000000E+0 -4.92999992E+1 4.21553432E-15
8.00000000E+0 -4.92000008E+1 4.35842074E-15
9.00000000E+0 -4.90999985E+1 4.50614963E-15
1.00000000E+1 -4.90000000E+1 4.65888619E-15
1.10000000E+1 -4.89000015E+1 4.81679981E-15
1.20000000E+1 -4.87999992E+1 4.98006542E-15
1.30000000E+1 -4.87000008E+1 5.14886553E-15
1.40000000E+1 -4.85999985E+1 5.32338693E-15
1.50000000E+1 -4.85000000E+1 5.50382358E-15
1.60000000E+1 -4.84000015E+1 5.69037623E-15
1.70000000E+1 -4.82999992E+1 5.88325241E-15
1.80000000E+1 -4.82000008E+1 6.08266599E-15
1.90000000E+1 -4.80999985E+1 6.28883847E-15
2.00000000E+1 -4.80000000E+1 6.50199939E-15
2.10000000E+1 -4.79000015E+1 6.72238551E-15
2.20000000E+1 -4.77999992E+1 6.95024160E-15
2.30000000E+1 -4.77000008E+1 7.18582095E-15
2.40000000E+1 -4.75999985E+1 7.42938443E-15
2.50000000E+1 -4.75000000E+1 7.68120478E-15
2.60000000E+1 -4.74000015E+1 7.94155984E-15

it goes on much more..............

the first column represents time, second voltage, and third current.

so I want to make voltage[n], current[n] where n represents the

number of data for voltage, current.....

Actually what makes it difficult is that I must change the data format
into double,

because they are all written in the format x.xxxxxE+-x

but maybe I can get through with that problem only if I know WHAT
FUNCTION TO USE.

Please at least could somebody tell me the functions in the fstream
class that

I need to use??

I am stuck with my program for several days, because of this part.

Searching in the web or books didn't help much;;

Oct 17 '06 #1
2 1322
<ra****@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
hi, I am having trouble with file input to the array I made in the
program.

1. I want to measure how much data are there in the file, and

2. put all the datas into array. (number of array is same as the number
of columns of data)

the file looks somewhat like this.

0.00000000E+0 -5.00000000E+1 3.33823774E-15
1.00000000E+0 -4.99000015E+1 3.45138779E-15
2.00000000E+0 -4.97999992E+1 3.56837278E-15
3.00000000E+0 -4.97000008E+1 3.68932315E-15
4.00000000E+0 -4.95999985E+1 3.81437317E-15
5.00000000E+0 -4.95000000E+1 3.94366216E-15
6.00000000E+0 -4.94000015E+1 4.07733285E-15
7.00000000E+0 -4.92999992E+1 4.21553432E-15
8.00000000E+0 -4.92000008E+1 4.35842074E-15
9.00000000E+0 -4.90999985E+1 4.50614963E-15
1.00000000E+1 -4.90000000E+1 4.65888619E-15
1.10000000E+1 -4.89000015E+1 4.81679981E-15
1.20000000E+1 -4.87999992E+1 4.98006542E-15
1.30000000E+1 -4.87000008E+1 5.14886553E-15
1.40000000E+1 -4.85999985E+1 5.32338693E-15
1.50000000E+1 -4.85000000E+1 5.50382358E-15
1.60000000E+1 -4.84000015E+1 5.69037623E-15
1.70000000E+1 -4.82999992E+1 5.88325241E-15
1.80000000E+1 -4.82000008E+1 6.08266599E-15
1.90000000E+1 -4.80999985E+1 6.28883847E-15
2.00000000E+1 -4.80000000E+1 6.50199939E-15
2.10000000E+1 -4.79000015E+1 6.72238551E-15
2.20000000E+1 -4.77999992E+1 6.95024160E-15
2.30000000E+1 -4.77000008E+1 7.18582095E-15
2.40000000E+1 -4.75999985E+1 7.42938443E-15
2.50000000E+1 -4.75000000E+1 7.68120478E-15
2.60000000E+1 -4.74000015E+1 7.94155984E-15

it goes on much more..............

the first column represents time, second voltage, and third current.

so I want to make voltage[n], current[n] where n represents the

number of data for voltage, current.....

Actually what makes it difficult is that I must change the data format
into double,

because they are all written in the format x.xxxxxE+-x

but maybe I can get through with that problem only if I know WHAT
FUNCTION TO USE.

Please at least could somebody tell me the functions in the fstream
class that

I need to use??

I am stuck with my program for several days, because of this part.

Searching in the web or books didn't help much;;
Using an ifstream (input file stream) you can just use >to load the number
into a double. It excepts the scientific format. I hope this isn't
homework, because here's the solution. At first I thought it was doing
something wrong because the output doesn't look like the input, then I
realized that
1.00000000E+0 -4.99000015E+1 3.45138779E-15
is indeed actually
1 -49.9 3.45139e-015

#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::ifstream DataFile( "C:/Temp Folder/somedata.txt" );
double d1, d2, d3;
int count = 0;
if ( DataFile.is_open() )
{
while ( DataFile >d1 >d2 >d3 )
{
std::cout << "Line " << ++count << " " << d1 << " " << d2 << " "
<< d3 << "\n";
}
DataFile.close();
std::cout << "Number of lines: " << count << std::endl;
}

std::string wait;
std::getline( std::cin, wait );

return 0;
}

Line 1 0 -50 3.33824e-015
Line 2 1 -49.9 3.45139e-015
Line 3 2 -49.8 3.56837e-015
Line 4 3 -49.7 3.68932e-015
Line 5 4 -49.6 3.81437e-015
Line 6 5 -49.5 3.94366e-015
Line 7 6 -49.4 4.07733e-015
Line 8 7 -49.3 4.21553e-015
Line 9 8 -49.2 4.35842e-015
Line 10 9 -49.1 4.50615e-015
Line 11 10 -49 4.65889e-015
Line 12 11 -48.9 4.8168e-015
Line 13 12 -48.8 4.98007e-015
Line 14 13 -48.7 5.14887e-015
Line 15 14 -48.6 5.32339e-015
Line 16 15 -48.5 5.50382e-015
Line 17 16 -48.4 5.69038e-015
Line 18 17 -48.3 5.88325e-015
Line 19 18 -48.2 6.08267e-015
Line 20 19 -48.1 6.28884e-015
Line 21 20 -48 6.502e-015
Line 22 21 -47.9 6.72239e-015
Line 23 22 -47.8 6.95024e-015
Line 24 23 -47.7 7.18582e-015
Line 25 24 -47.6 7.42938e-015
Line 26 25 -47.5 7.6812e-015
Line 27 26 -47.4 7.94156e-015
Number of lines: 27
Oct 17 '06 #2
Thank you so much for the reply.

It really works even after I changed it for my program.

The part " while ( DataFile >d1 >d2 >d3 )"

was impossible for me to make.

I didn't know it was that simple!!
Jim Langston wrote:
<ra****@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
hi, I am having trouble with file input to the array I made in the
program.

1. I want to measure how much data are there in the file, and

2. put all the datas into array. (number of array is same as the number
of columns of data)

the file looks somewhat like this.

0.00000000E+0 -5.00000000E+1 3.33823774E-15
1.00000000E+0 -4.99000015E+1 3.45138779E-15
2.00000000E+0 -4.97999992E+1 3.56837278E-15
3.00000000E+0 -4.97000008E+1 3.68932315E-15
4.00000000E+0 -4.95999985E+1 3.81437317E-15
5.00000000E+0 -4.95000000E+1 3.94366216E-15
6.00000000E+0 -4.94000015E+1 4.07733285E-15
7.00000000E+0 -4.92999992E+1 4.21553432E-15
8.00000000E+0 -4.92000008E+1 4.35842074E-15
9.00000000E+0 -4.90999985E+1 4.50614963E-15
1.00000000E+1 -4.90000000E+1 4.65888619E-15
1.10000000E+1 -4.89000015E+1 4.81679981E-15
1.20000000E+1 -4.87999992E+1 4.98006542E-15
1.30000000E+1 -4.87000008E+1 5.14886553E-15
1.40000000E+1 -4.85999985E+1 5.32338693E-15
1.50000000E+1 -4.85000000E+1 5.50382358E-15
1.60000000E+1 -4.84000015E+1 5.69037623E-15
1.70000000E+1 -4.82999992E+1 5.88325241E-15
1.80000000E+1 -4.82000008E+1 6.08266599E-15
1.90000000E+1 -4.80999985E+1 6.28883847E-15
2.00000000E+1 -4.80000000E+1 6.50199939E-15
2.10000000E+1 -4.79000015E+1 6.72238551E-15
2.20000000E+1 -4.77999992E+1 6.95024160E-15
2.30000000E+1 -4.77000008E+1 7.18582095E-15
2.40000000E+1 -4.75999985E+1 7.42938443E-15
2.50000000E+1 -4.75000000E+1 7.68120478E-15
2.60000000E+1 -4.74000015E+1 7.94155984E-15

it goes on much more..............

the first column represents time, second voltage, and third current.

so I want to make voltage[n], current[n] where n represents the

number of data for voltage, current.....

Actually what makes it difficult is that I must change the data format
into double,

because they are all written in the format x.xxxxxE+-x

but maybe I can get through with that problem only if I know WHAT
FUNCTION TO USE.

Please at least could somebody tell me the functions in the fstream
class that

I need to use??

I am stuck with my program for several days, because of this part.

Searching in the web or books didn't help much;;

Using an ifstream (input file stream) you can just use >to load the number
into a double. It excepts the scientific format. I hope this isn't
homework, because here's the solution. At first I thought it was doing
something wrong because the output doesn't look like the input, then I
realized that
1.00000000E+0 -4.99000015E+1 3.45138779E-15
is indeed actually
1 -49.9 3.45139e-015

#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::ifstream DataFile( "C:/Temp Folder/somedata.txt" );
double d1, d2, d3;
int count = 0;
if ( DataFile.is_open() )
{
while ( DataFile >d1 >d2 >d3 )
{
std::cout << "Line " << ++count << " " << d1 << " " << d2 << " "
<< d3 << "\n";
}
DataFile.close();
std::cout << "Number of lines: " << count << std::endl;
}

std::string wait;
std::getline( std::cin, wait );

return 0;
}

Line 1 0 -50 3.33824e-015
Line 2 1 -49.9 3.45139e-015
Line 3 2 -49.8 3.56837e-015
Line 4 3 -49.7 3.68932e-015
Line 5 4 -49.6 3.81437e-015
Line 6 5 -49.5 3.94366e-015
Line 7 6 -49.4 4.07733e-015
Line 8 7 -49.3 4.21553e-015
Line 9 8 -49.2 4.35842e-015
Line 10 9 -49.1 4.50615e-015
Line 11 10 -49 4.65889e-015
Line 12 11 -48.9 4.8168e-015
Line 13 12 -48.8 4.98007e-015
Line 14 13 -48.7 5.14887e-015
Line 15 14 -48.6 5.32339e-015
Line 16 15 -48.5 5.50382e-015
Line 17 16 -48.4 5.69038e-015
Line 18 17 -48.3 5.88325e-015
Line 19 18 -48.2 6.08267e-015
Line 20 19 -48.1 6.28884e-015
Line 21 20 -48 6.502e-015
Line 22 21 -47.9 6.72239e-015
Line 23 22 -47.8 6.95024e-015
Line 24 23 -47.7 7.18582e-015
Line 25 24 -47.6 7.42938e-015
Line 26 25 -47.5 7.6812e-015
Line 27 26 -47.4 7.94156e-015
Number of lines: 27
Oct 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by matt | last post: by
1 post views Thread by Rahul | last post: by
22 posts views Thread by KitKat | last post: by
8 posts views Thread by skumar434 | last post: by
1 post views Thread by al2004 | last post: by
22 posts views Thread by Amali | last post: by
2 posts views Thread by =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post: by
2 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.