Arrays and lines

Ok, I know arrays can store numbers and characters, but I'm having difficulty understanding how to make the program (when a file is open for input)shift to the next line when it is nessesary. I have looked in the forums but with no luck. Also, I would like to know how to differentiat between integers and char/strings. For example: we have and array {12 23 31}. I am pretty certain sp[aces arent integers, so we cant create an "if(i = '')" because an integer isn't a character, even though the character can go into the array. How could i write my program to store each number as a variable (assuming that the spaces separate each number)? How do I make the array store each number into a line, and how do i write my program so it will automatically go to the next line?

I want my program to be able to get numbers from a data file that looks like this:

12 34 100
110 113 58
1 2 453
2 30 993

I want it to be able to create an array with those numbers and display each number on a line. ( what i'm really asking for is an example of how to do this because i have no idea how) I've tried to do this with an array before, using what i've heard and seen on the forums and tutorials, but i think i need some more instruction because i am not really grasping it. I really appreciate your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
    You need to get my String Class from...

    http://www.jose.it-berater.org/smfforum/index.php?topic=4176.30

    Its in Post #42 (Reply #42).  Copy the Strings.h and Strings.cpp
    files and include them with this code.

    Make a text file with the numbers from your post, i.e.,...

    12 34 100
    110 113 58
    1 2 453
    2 30 993

    This is a space delimited text file.  Save it in the folder with
    the below program, and name it Data.txt.

    The program will read in each line and parse it for you.  If you
    want to convert the strings to integers there are functions for
    that.
*/

#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>
#include <cstdio>
#include "Strings.h"

int main()
{
 int iParseCount=0,iRet=0;
 String* pStrs=NULL;
 TCHAR szLine[160];
 FILE* fp=NULL;
 String s1;

 fp=fopen("Data.txt","r");
 if(fp)
 {
    do
    {
       memset(szLine,0,160*sizeof(TCHAR));
       iRet=(int)_fgetts(szLine,160,fp);
       if(!iRet)
          break;
       s1=szLine;
       iParseCount=s1.ParseCount(_T(' '));
       printf("iParseCount = %d\n\n",iParseCount);
       pStrs=new String[iParseCount];
       s1.Parse(pStrs,_T(' '));
       for(int i=0; i<iParseCount; i++)
           pStrs[i].Print(true);
       delete [] pStrs;
       printf("\n");
    }while(1);
    fclose(fp);
 }
 getchar();

 return 0;
}

/*
Output:
==========
iParseCount = 3

12
34
100


iParseCount = 3

110
113
58


iParseCount = 3

1
2
453


iParseCount = 3

2
30
993
*/
uuuhhh.... I really just want to learn how to write it myself... lol. Thanks for the offer though.
An array can't grow dynamically so you need to declare one large enough to hold all the lines in your text file, or else use a dynamic storage type like a vector. Here's a quick and dirty way to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream inFile("yourFileName.txt");
    int numArray[100]; // 100 is just arbitrary; it needs to be large enough to hold all your data
    for(int i = 0; i < 99; ++i)
        numArray[i] = -999; // initializing to tell when to stop printing later

    int arrayPos = 0;
    while(!inFile.eof())
    {
        inFile >> numArray[arrayPos];
        ++arrayPos;
    }
    inFile.close();

    arrayPos = 0;
    while(numArray[arrayPos] != -999)
    {
        cout << numArray[arrayPos] << endl;
        ++arrayPos;
    }

    return 0;
}
So, I have a question. How can you separate the numbers once the array has been filled with our file's numbers? When i declare it as an integer, it excludes the spaces creating a long line of numbers. Would an array declared as char work? But how would i be able to use the value as an integer?

These are the things that i'm having trouble with. Thank you for that example code, I appreciate your time and effort.
You made it plain you don't want my code but you're getting it anyway (I was intrigued by the problem so I worked it out just for my own curiousity). Just ignore it if you like (However, it has everything in it you want to know).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
   Program Demonstrates Reading Lines Of Space Delimited Integers In From A Text
   File, Parsing The Line Into Its Constituent Integers, and Storing The Binary
   Representation In Dynamically Allocated Memory Using Two Dimensional Array
   Syntax.

   Here is the nature of the problem this program was designed to solve.
   Suppose one has a space delimited text file of an arbitrary number of lines
   and each line contains an arbitrary and varying number of integers, for
   example...

   1 3 5 7 9
   2 4 6 8 10 12 14
   3 6 9
   4 8 12 16 18 20 22
   5 10

   As can be seen there are five lines in this data, and the 1st line has five
   integers in it, the 2nd line seven, etc.  The below code will handle this;
   only issue being if there are more than 1024 characters on a line you'll
   need to adjust MAX_LINES accordingly.  To run this code you'll need to
   obtain my String Class from post #42 at...

   http://www.jose.it-berater.org/smfforum/index.php?topic=4176.30

*/
#define  UNICODE
#define  _UNICODE
#include <windows.h>
#include <tchar.h>
#include <cstdio>
#include "Strings.h"
#define  arInt(i,j) (pInts[(i)*(columns)+(j)])
#define  MAX_LINE   1024


bool blnDimensions(int& iRows, int& iCols, FILE* fp)  //When this function returns
{                                                     //you'll know the maximum
 TCHAR szLine[MAX_LINE];                              //dimensions of the array
 int iParseCount=0;                                   //you'll need to contain the
 TCHAR* pChar=NULL;                                   //data in your file in terms
 String strLine;                                      //of the number of rows and
                                                      //number of columns.  iRows and
 rewind(fp);                                          //iCols were passed by
 while(!feof(fp))                                     //reference and initialized
 {                                                    //with 0 in main().  rewind()
    pChar=_fgetts(szLine,160,fp);                     //sets the FILE* back to the
    strLine=szLine;                                   //beginning of the file.
    iParseCount=strLine.ParseCount(' ');              //String::ParseCount() is a
    if(iParseCount>iCols)                             //member function of my String
       iCols=iParseCount;                             //Class which returns the
    iRows++;                                          //number of sub-strings
 }                                                    //delimited by the delimiter
 rewind(fp);                                          //of your choice - here a ' ',
 if(iRows)                                            //i.e., space character.  If
    return true;                                      //iRows is not incremented by
 else                                                 //reading a line, the function
    return false;                                     //returns false; otherwise
}                                                     //true.


int main()                                    //What we're going to do here is open
{                                             //Data1.txt and call blnDimensions()
 int iRows=0,iCols=0,iParseCount=0,i,j;       //to find how many lines are in the
 TCHAR szLine[MAX_LINE];                      //file, and what the maximum number of
 int columns,iCtr=0;                          //integers on any given line is.  With
 String* pStrs=NULL;                          //that info we can do a memory alloc-
 int* pInts=NULL;                             //ation for a block of memory large
 String strLine;                              //enough to hold all the lines of
 FILE* fp=NULL;                               //varying numbers of integers.  Unfor-
                                              //tunately, we'll have to assume every
 fp=_tfopen(_T("Data1.txt"),_T("r"));         //line contains the number of integers
 if(fp)                                       //of the longest line.  Nothing we can
 {                                            //do about that with this scheme.
    if(blnDimensions(iRows,iCols,fp))         //Let's allot an extra slot though to
    {                                         //hold the count of integers in each
       _tprintf(_T("iRows = %d\n"),iRows);    //line.  We'll store that number in the
       _tprintf(_T("iCols = %d\n\n"),iCols);  //zeroth array element of each line.
       columns=iCols+1;                       //For example, in the data above, zero
       pInts=(int*)GlobalAlloc                //based line #'s 1 and 3 each contain
       (                                      //7 integers.  So arInt[1,0] and
        GPTR,iRows*(columns)*sizeof(int)      //arInt[3,0] will be set to 7.  This
       );                                     //will allow for easy iteration of the
       if(pInts)                              //numbers on each line.  We'll know
       {                                      //this number because when we read in
          while(!feof(fp))                    //each line String::ParseCount() will
          {                                                //determine the number of
             memset(szLine,0,MAX_LINE*sizeof(TCHAR));      //space delimited integers
             _fgetts(szLine,MAX_LINE,fp);                  //in each line.  Note in
             strLine=szLine;                               //the arInt() macro above
             iParseCount=strLine.ParseCount(_T(' '));      //we'll need to send
             pStrs=new String[iParseCount];                //columns into it with one
             strLine.Parse(pStrs,' ');                     //more than the maximum
             arInt(iCtr,0)=iParseCount;                    //number of integers on
             for(int j=1; j<=iParseCount; j++)             //a line because we're
                 arInt(iCtr,j)=_ttoi(pStrs[j-1].lpStr());  //storing the number of
             delete [] pStrs;                              //integers in each line
             iCtr++;                                       //in arInt(i,0), and this
          }                                                //will require one extra
          _tprintf(_T("Successfully Read In All Data. ")); //memory slot for each
          _tprintf(_T(" Will Now Dump Data....\n\n\n"));   //line.
          String strDevider(67,_T('='));
          _tprintf(_T("arInt[i,0]\t\t\t   Data\n"));
          strDevider.Print(true);
          for(i=0; i<iCtr; i++)
          {
              _tprintf(_T("%d\t\t"),arInt(i,0));
              for(j=0; j<arInt(i,0); j++)
                  _tprintf(_T("%d\t"),arInt(i,j+1));
              _tprintf(_T("\n"));
          }
          GlobalFree(pInts);
       }
    }
    fclose(fp);
 }
 getchar();

 return 0;
}

/*
Original Data
==================
1 3 5 7 9
2 4 6 8 10 12 14
3 6 9
4 8 12 16 18 20 22
5 10
*/

/*
Program Output Assumming Above Data...

iRows = 5
iCols = 7

Successfully Read In All Data...
Will Now Dump Data....


arInt[i,0]                         Data
===================================================================
5               1       3       5       7       9
7               2       4       6       8       10      12      14
3               3       6       9
7               4       8       12      16      18      20      22
2               5       10
*/
Thanks. I appreciate your help and will look over your code.

Topic archived. No new replies allowed.