Originally Posted by
Salem
Code:
ifstream in("myfile.txt");
int var;
while ( in >> var ) {
cout << "Read in " << var << endl;
}
wow, this one's realy simple, thanks
OK, so now that I know this I want to read form a file like this:
Code:
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
10 100
11 110
12 120
13 130
14 140
15 150
16 160
and, I want only the second numbers (10, 20, 30, ...).
so I've made this:
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ int i,j,var[40], d[40];
i=0;
j=0;
ifstream in("example.txt");
while ( in >> var[i] )
{
cout << "Read in " << var[i] << endl;
if(i%2)
{
d[j]=var[i];
j++;
}
i++;
}
cout<<endl;
for(i=0;i<j;i++)
{
cout<<d[i]<<endl;
}
cout<<endl;
system("PAUSE");
return 0;
}
it works fine, but I'm not sure is there maybe a simpler way to do this, maybe while reading?
and, also, how to make this into a function?
I'll have to read the data from two different files, and store them is two different series.
what I don't know is wether the series then need to be globaly defined or not, and what arguments should function have.
what I've written here is an outline of what it should look like but I'm unsure about the part with the question marks.
Code:
#include<iostream>
#include<fstream>
#...
using namespace std;
function read(????)
{
this stuff for reading the file
return ???
}
int main()
{
read(dat1.txt????, a[i]????);
read(dat2.txt????, b[i]????);
...
}