arrays w/ data files

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.
Kal130Asked:
Who is Participating?
 
DariuzkConnect With a Mentor Commented:
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
 
venkateshwarrCommented:
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
 
Kal130Author Commented:
is there a simpler way to do that becasue i havent learned some of those functions yet
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Kal130Author Commented:
with this, i have 2 arrays that i can compare to eachother or to another array and then output a desired operation?
0
 
Kal130Author Commented:
i dont know the number of entries in the input file, this only shows 10 of them, their could be 100's
0
 
Kal130Author Commented:
actually nevermind, no need to answer, i think i misinterpreted the question

thanks anyway
0
 
DariuzkCommented:
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
 
novitiateCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.