Solved

ask content of the file into array

Posted on 2004-10-14
7
225 Views
Last Modified: 2010-04-01
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;

      }



}
0
Comment
Question by:tom_mk
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 500 total points
ID: 12306314
Code to fill an array would be.

int i=0;
while (in) { // should be equal to while (!in.eof())
   in >> ar[i];
   ++i;
}

I am not sure what p_a() and p_b () are, os I can't really comment on a solution within your code sample.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12306353
You can also use the fact that your ofstream bool operator() returns false when you hit the end of file.

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<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12306366
Actually SteH's does that too. I should put my reading glasses on.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tom_mk
ID: 12306372
according to u guys,

will the space ' ' be filled into the array?
cos i only need the number in the array
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12306390
By using operator>> you skip the white space.
0
 

Author Comment

by:tom_mk
ID: 12306397
thx man..

i been away from c++ for too long...hahah
0
 
LVL 13

Expert Comment

by:SteH
ID: 12306421
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now