Solved

Open Text File of Numbers into an Array

Posted on 1998-12-06
3
183 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
Comment Utility
int i;
while (cin >> i) {
    // do stuff with i
}

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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

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

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…
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 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 learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

7 Experts available now in Live!

Get 1:1 Help Now