?
Solved

arrays w/ data files

Posted on 2005-04-13
8
Medium Priority
?
252 Views
Last Modified: 2010-04-01
if i have a data file with values like:
1 2
2 4
3 3
1 5

how can i put the first column into an array, then the next column into a differerent array? (all values are of type int)  i cannot use pointers to do this.
the number of entries in the data file is unknown.
0
Comment
Question by:Kal130
8 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 13779129
you can do something like this...

while(!feof(fp))
{
  fscanf(fp, "%d %d",&num1, &num2);
  arr1[i] = num1;
  arr2[i] = num2;
}

your result will be in arrays arr1 and arr2
0
 

Author Comment

by:Kal130
ID: 13779148
is there a simpler way to do that becasue i havent learned some of those functions yet
0
 
LVL 2

Accepted Solution

by:
Dariuzk earned 150 total points
ID: 13779295
Hi,
or in more C++ way ;)

#include <iostream>
#include <fstream>

using namespace std;

int main(){
      int a[10], b[10], count = 0, col1, col2;

      for(int i=0; i<10; i++)
            a[i] = b[i] = 0;

      ifstream inFile;
      inFile.open("exm1.txt");
      
      while(!inFile.eof())
      {
            inFile >> col1 >> col2;
            if (inFile.good())
            {
                  a[count] = col1;
                  b[count] = col2;
                  count++;
            }
      }
      
      inFile.close();

      for(int i=0; i<10; i++)
            cout << " a[" << i << "] = " << a[i]
                 << " b[" << i << "] = " << b[i]
                 << endl;
}
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:Kal130
ID: 13779340
with this, i have 2 arrays that i can compare to eachother or to another array and then output a desired operation?
0
 

Author Comment

by:Kal130
ID: 13779350
i dont know the number of entries in the input file, this only shows 10 of them, their could be 100's
0
 

Author Comment

by:Kal130
ID: 13779654
actually nevermind, no need to answer, i think i misinterpreted the question

thanks anyway
0
 
LVL 2

Expert Comment

by:Dariuzk
ID: 13779743
You can output array like this:
 for(int i=0; i<count; i++)
          cout << " a[" << i << "] = " << a[i]
               << " b[" << i << "] = " << b[i]
               << endl;
so in count you have number of elements in array.
You can do :

inFile >> col1;
if (inFile.good())
{
               a[count] = col1;
               count1++;
}
inFile >> col2;
if (inFile.good())
{
               b[count] = col2;
               count2++;
}

if you whant to be able to read diferent number of numbers into a and b arrays.
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13779889
then you need to use one of those stl collections


#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;

typedef vector<int> IntVector;

void print(int val)
{
      cout << val << "\t";
}
int main(){

      IntVector a, b;
      int count = 1, col1, col2;
      
      
      ifstream inFile;
      inFile.open("c:\\temp\\data.txt");
      
      while(!inFile.eof())
      {
            inFile >> col1 >> col2;
            if (inFile.good())
            {
                  a.push_back(col1);
                  b.push_back(col2);
                  count++;
            }
      }
      
      inFile.close();
      
      for_each(a.begin(), a.end(), print);
      cout << endl;
      for_each(b.begin(), b.end(), print);
      cout << endl;
      return 0;
}

_novi_
0

Featured Post

Independent Software Vendors: 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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

864 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