Hi all, i got trouble reading array file
sample file to be read:
---------------------------------
START 3
. 000 2 100 . 000 . 000
0 100 1 050 . 000 . 000
. 000 . 000 . 000 . 000
. 000 . 000 . 000 . 000
END
START 2
. 000 2 100 . 000 . 000
0 100 1 050 . 000 . 000
. 000 . 000 . 000 . 000
. 000 . 000 . 000 . 000
END
----------------------------------
The problem is when I try to read char, I just can get space
it is weird and hope somebody can help me out
thanx in advance!!
my code:
Code:
int main()
{
char label[6]; //String to read in the START/IN label
char descriptor; //Char to read in the descriptor
int descriptorVal; //Int to read in the descriptor's value
int numTurns; //Number of turns read from the input file
int numRows; //Number of rows in the matrix
int numCols; //Number of columns in the matrix
int tempNumCols; //Copy of 'numCols'
char descriptorArray[MAXDIM][MAXDIM]; //Matrix to hold the descriptor map
int valueArray[MAXDIM][MAXDIM]; //Matrix to hold the descriptor values
int x, y, index;
FILE *infile;
infile = fopen("a.dat","r");
if (infile == NULL)
{
printf("Error. File does not exist.\n"
"Terminating Program.\n\n\n");
}
else
{
// Gather the input and play the game as long as there is input to process:
while(fscanf(infile, "%s", &label) != EOF && !strcmp(label, "START"))
{
// Reinitialize re-used variables to allow them to be re-used if necessary:
numRows = 0;
numCols = 0;
tempNumCols = 0;
// Reinitalize the arrays to allow them to be re-used if necessary:
for(x = 0; x < MAXDIM; x++)
for(y = 0; y < MAXDIM; y++)
{
descriptorArray[x][y] = '\0';
valueArray[x][y] = 0;
}
// Get the number of turns:
fscanf(infile, "%d", &numTurns);
// Get the map, partitioning the descriptor and value into separate arrays:
for(index = 0; index < MAXDIM; index++)
{
//As long as we are not at the end, get the next descriptor/value pair:
while(!foundEND(infile))
{
//If we found a descriptor/value pair, store them in the appropriate arrays
if(fscanf(infile, "%c ", &descriptor) && fscanf(infile, "%d ", &descriptorVal))
{
descriptorArray[numRows][tempNumCols] = descriptor;
valueArray[numRows][tempNumCols] = descriptorVal;
numCols++;
tempNumCols++;
printf("%c %d",descriptor, descriptorVal);
//If we've come to the end of the line, break from the loop:
if(foundEOL(infile))
break;
}
}
//Increment the number of rows because we are at the end of the row:
numRows++;
//IF we are not at the end of the data set, restart the loop:
if(!foundEND(infile))
{
index = 0;
tempNumCols = 0;
}
//ELSE we are at the end of the data set, so break out:
else break;
}
//Calculate the number of columns of the matrix:
numCols = numCols / numRows;
//Ensure that the dimensions of the map are valid:
if((numRows > 20) || (numCols > 20))
{
printf("Error: The map is too large!\n");
return 1;
}
fscanf(infile, "%s", label);
}
}
fclose(infile);
return 0;
}
////////////////////////////////////////////////////////////
// Search the next string in the input for a 'END' label: //
////////////////////////////////////////////////////////////
int foundEND(FILE *in)
{
char endString[4] = "\0";
fscanf(in, "%s", &endString);
if(!strcmp(endString, "END"))
return 1;
else
{
return 0;
}
}
///////////////////////////////////////////////////////////
// Search the next character in the input for a newline: //
///////////////////////////////////////////////////////////
int foundEOL(FILE *in)
{
char letter;
int spaces = 0,
condition = 0, i = 0, k;
while (spaces == 0)
{
letter = fgetc(in);
if ( letter != ' ' )
spaces = 1;
}
if ('\n' == letter)
condition = MY_EOL;
else
{
while (!isspace(letter) )
{
letter = fgetc(in);
if (letter == EOF)
{
condition = MY_EOF;
}
}
}
if (letter == '\n')
condition = MY_EOL;
return condition;
}