Hi
If I have a data file with numbers that I want to store in an array. What C++ finction can I use?
ie.
Data_file
# velocity
3
4.5
6.1
-3
...N
and i want to store these in array x[N]
ThanksCode:int main(){ double x[N]; }
Shuo
Hi
If I have a data file with numbers that I want to store in an array. What C++ finction can I use?
ie.
Data_file
# velocity
3
4.5
6.1
-3
...N
and i want to store these in array x[N]
ThanksCode:int main(){ double x[N]; }
Shuo
Code:int main(){ double x[N]; ifstream in("file.txt"); int i = 0; while (i < N && in >> x[i]) { i++; } }
what headers do i need for ifstream?
I included
#include <fstream>
#include <iostream>
Don't forget
orCode:using namespace std;
For ifstream, the header fstream is required.Code:std::ifstream
How do I skip variable headers in the data file like
Velocity
.5
4
,,,
Density
1.2
0.3
...
yes, I want to store them in arrays a[] and b[].
One way is to use getline() to skip a line:
Code:#include <string> . . std::string line; getline(in, line);
what about mutiple lines do i have to repeat the getline() each time?
Thanks
Yes. The function reads one line at a time.
Is there a way of skipping a defined number of lines?
The easiest way is just reading a specified number of lines. Otherwise you can seek in the file, but it's by position, not lines, so it's not very efficient, since you have to manually check for line ends and count and stuff.
There's no function to do what you want, at least none that I know of.