Use
fgets - C++ Reference[
^] to read the text file line by line into the buffer. Then parse each line to get the column elements. This depends on the format of your text files. Parsing function candidates are
sscanf - C++ Reference[
^] and
strpbrk - C++ Reference[
^] or parsing for delimiters and using
atoi()
and
atof()
for numeric values.
A basic implementation:
int elements[MAX_ROWS][MAX_COLUMNS];
FILE f2 = fopen("Cost.txt" , "r");
if (NULL != f2)
{
int row = 0;
char lineBuf[MAX_LINE_LENGTH];
while (NULL != fgets(buf, sizeof(lineBuf), f2))
{
int col = 0;
const char *colData = lineBuf;
elements[row][col++] = atoi(colData);
row++;
}
fclose(f2);
}