Solved

ask content of the file into array

Posted on 2004-10-14
7
222 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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…
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.

707 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

20 Experts available now in Live!

Get 1:1 Help Now