Solved

ask content of the file into array

Posted on 2004-10-14
7
229 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 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