Solved

Open Text File of Numbers into an Array

Posted on 1998-12-06
3
186 Views
Last Modified: 2010-04-02
I have a text file with a header row of variables and the rest as numbers in columns.  I can open the file using getline but it reads the number rows as a string.  What I then have to do is break each string of row numbers down by looking for tab spaces, converting the string to a number, and then saving it into an array.  Can somebody please tell me the easiest way to open a text file of numbers with various rows and columns into an array?

Thanks
0
Comment
Question by:dmontgom
3 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1179457
int i;
while (cin >> i) {
    // do stuff with i
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1179458
i.e what yonat is saying is that the standard library is capable of reading the ASCII data and converting the digits into integers (or other numerical types)  so don't read the data in as ASCII characters, read it in as the numerical type you want.
0
 
LVL 1

Accepted Solution

by:
billyh earned 100 total points
ID: 1179459
Hope this helps:

#include "fstream.h"
#include "iostream.h"

#ifndef MAXLEN
      #define MAXLEN 45
#endif

void main()
{
      char header_row[MAXLEN];
      fstream in_text_file;
      int local_var = 0, index = 0, int_arr[MAXLEN];

      in_text_file.open("text.dat",ios::in);

      while(!in_text_file.eof())
      {
            if (!local_var)
            {
                  in_text_file.getline(header_row,MAXLEN,'\n');
                  ++local_var;
            }
            else
            {
                  in_text_file >> local_var;
                  int_arr[index] = local_var;
                  ++index;
            }
      }

      in_text_file.close();
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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