Thread: Read Array pro!!Plz help!!

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    21

    Read Array pro!!Plz help!!

    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;
    }
    Last edited by Supra; 03-02-2002 at 08:05 PM.

  2. #2
    Unregistered
    Guest

    Unhappy

    No body helps....?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because a 'space' is a 'char'.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 08:42 PM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  4. read float array from file
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2002, 04:20 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM