Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open Text File of Numbers into an Array

Posted on 1998-12-06
3
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

730 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