473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1477
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
2
by: =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post by:
I'm creating a doc project for my c# program. I've done this before but this time sonething is wrong. I build my doc project and is succeeds but when I open the help file, there is no documentation...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.