Solved

ask content of the file into array

Posted on 2004-10-14
7
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

690 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