[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ask content of the file into array

Posted on 2004-10-14
7
Medium Priority
?
246 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 2000 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
Industry Leaders: 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

649 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