tom_mk
asked on
ask content of the file into array
hi
this is wat my file looks like
6.34 2.0 1.0 10 20 10 10 -20 10 10 10 20 0 20 10 -10 50 -40 50 10
i wanna read that line into my prog
and put each number into array, so that i can call each of them instantly.
this is wat i trying(bleow), but i kina sruck,
any1 can suggest me a code of how to do this.
i'd be really appreciate. (i using : VC++ 6.0)
Thx
Tom
void load(char *filename)
{
ifstream in;
ofstream out;
in.open(filename);
if(in.fail())
{
cout << "fail2open\n";
exit(1);
}
else
{
cout << "opened";
cout << "\n\n\n";
}
char next;
while(!in.eof())
{
cout << next;
//cout << "\n";
if(next == 'a')
{
p_a();
}
if(next == 'b')
{
p_b();
}
in.get(next);
//in.getline(next,3); //where 100 is the size of the array
//cout << next << endl;
}
}
this is wat my file looks like
6.34 2.0 1.0 10 20 10 10 -20 10 10 10 20 0 20 10 -10 50 -40 50 10
i wanna read that line into my prog
and put each number into array, so that i can call each of them instantly.
this is wat i trying(bleow), but i kina sruck,
any1 can suggest me a code of how to do this.
i'd be really appreciate. (i using : VC++ 6.0)
Thx
Tom
void load(char *filename)
{
ifstream in;
ofstream out;
in.open(filename);
if(in.fail())
{
cout << "fail2open\n";
exit(1);
}
else
{
cout << "opened";
cout << "\n\n\n";
}
char next;
while(!in.eof())
{
cout << next;
//cout << "\n";
if(next == 'a')
{
p_a();
}
if(next == 'b')
{
p_b();
}
in.get(next);
//in.getline(next,3); //where 100 is the size of the array
//cout << next << endl;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually SteH's does that too. I should put my reading glasses on.
ASKER
according to u guys,
will the space ' ' be filled into the array?
cos i only need the number in the array
will the space ' ' be filled into the array?
cos i only need the number in the array
By using operator>> you skip the white space.
ASKER
thx man..
i been away from c++ for too long...hahah
i been away from c++ for too long...hahah
I assumed that "ar" is a array of numbers (doubles in this case). So it can't contain characters any more.
Whitespaces will be removed by all ways of reading numbers from a file or string I know. But as rstaveley pointed out >> is removing them in any case. So if you need strings from that line you need to define
string ar[100];
in my sample or
vector<string> dList; // the name should perhaps become sList in that case
string tmp;
in rstaveleys case.
Whitespaces will be removed by all ways of reading numbers from a file or string I know. But as rstaveley pointed out >> is removing them in any case. So if you need strings from that line you need to define
string ar[100];
in my sample or
vector<string> dList; // the name should perhaps become sList in that case
string tmp;
in rstaveleys case.
e.g.
--------8<--------
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc,const char* argv[])
{
if (argc != 2) return (cerr << "Usage: " << *argv << " {filename}\n"),1;
ifstream fin(*++argv);if (!argv) return (cerr << "Error: Unable to open " << *argv << '\n'),1;
vector<double> dList;
double tmp;
while (fin >> tmp) /* fin is false at eof */
dList.push_back(tmp);
for (int i = 0;i < dList.size();++i)
cout << "Value[" << i << "] is " << dList[i] << '\n';
}
--------8<--------