Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a Windows text file with the following info inside it:
25 05 38 26 53 04
07 45 50 33 19 34
55 25 21 30 09 39
26 11 30 12 13 41
32 23 44 11 50 39
45 30 07 44 55 54
21 10 35 46 48 27
52 41 05 53 11 50
40 38 17 43 10 54
45 27 29 12 39 31
24 42 38 02 18 09
13 43 28 06 53 30
45 47 29 30 53 13
38 45 28 48 47 36
25 34 18 06 07 55

How can I code them to break that info apart into six columns and put each column into their own array?
[array1] [array2] [array3] [array4] [array5] [array6]
   25	    05	     38	      26       53	    04
   07	    45	     50	      33       19	    34
   55	    25	     21	      30       09	    39
   26	    11	     30	      12       13	    41
   32	    23	     44	      11       05	    39
   45	    30	     07	      44       55	    54
   21	    10	     35	      46       48	    27
   52	    41	     05	      53       11	    50
   40	    38	     17	      43       10	    54
   45	    27	     29	      12       39	    31
   24	    42	     38	      02       18	    09
   13	    43	     28	      06       53	    30
   45	    47	     29	      30       53	    13
   38	    45	     28	      48       47	    36
   25	    34	     18	      06       07	    55


What I have tried:

I'm pretty much stumped at this point...
Posted
Updated 5-Feb-21 1:47am

Read the file line by line using the ifstream class. As you read each line into a char array you can use the C library strtok methods to extract each field one by one, convert it to a numeric value if necessary, and add it to the relevant array.

See <fstream> - C++ Reference[^].
 
Share this answer
 
v4
Comments
Rick York 5-Feb-21 11:31am    
That's what I've done a few hundred times in the past. I use fgets but it's essentially the same.
Richard MacCutchan 5-Feb-21 11:45am    
I am trying to discipline myself to use the STL classes rather than the old C run-times. But I fell at the second hurdle :( .
I would use std::vector instead of C-like arrays.
For instance, the following program
C++
#include<iostream>
#include <vector>
#include <sstream>

using  namespace std;

using vint = vector<int>;

int main()
{
  vector < vint > v = { vint{}, vint{}, vint{}, vint{}, vint{}, vint{} };

  string line;
  while ( getline(cin, line) )
  {
    istringstream iss(line);
    for ( auto & x : v )
    {
      int i;
      iss >> i;
      x.push_back(i);
    }
  }

  for (size_t n = 0; n < v.size(); ++n)
  {
    cout << "v[" << n << "] = ";
    for ( auto i : v[n] )
    {
      cout << i << " ";
    }
    cout << "\n";
  }
}


outputs

v[0] = 25 7 55 26 32 45 21 52 40 45 24 13 45 38 25 
v[1] = 5 45 25 11 23 30 10 41 38 27 42 43 47 45 34 
v[2] = 38 50 21 30 44 7 35 5 17 29 38 28 29 28 18 
v[3] = 26 33 30 12 11 44 46 53 43 12 2 6 30 48 6 
v[4] = 53 19 9 13 50 55 48 11 10 39 18 53 53 47 7 
v[5] = 4 34 39 41 39 54 27 50 54 31 9 30 13 36 55 


when fed with your input file (provided by standard input redirection).
 
Share this answer
 
v2
Comments
Shao Voon Wong 5-Feb-21 8:12am    
Brilliant!
CPallini 5-Feb-21 10:22am    
Thanks :-)
kangkongflea 5-Feb-21 8:23am    
To @CPallini
Um, what about reading the text file? Where does the ifstream line go in, as suggested by @RichardMacCutchan
CPallini 5-Feb-21 10:22am    
To use fstream just add
#include <fstream>
at the top of your source file and then replace:
while ( getline(cin, line) )
with (assuming your text file is called "input.txt"):
ifstream ifs("input.txt");
while ( getline(ifs, line) )
kangkongflea 6-Feb-21 0:19am    
Regarding the code, can that be ported to C++ and Visual C++?
Kinda looks like standard C...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900