Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open Text File of Numbers into an Array

Posted on 1998-12-06
3
Medium Priority
?
190 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 400 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

Industry Leaders: 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

926 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