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
*/
| |