Solved

Open Text File of Numbers into an Array

Posted on 1998-12-06
3
187 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 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 104
How to split this in C++ 4 129
draw a Christmas tree by using a nested loop? 26 105
Precision Problem in C++ 7 67
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 clear a vector as well as how to detect empty vectors in C++.

734 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