Exemple #1
0
int loadArray(char *inFileName, char ***array, int *count, int *capacity)
{
    FILE *inFile; /* file stream */
    char word[WORD_LENGTH];  /* this is the ONLY auto array we'll need */

    if ((inFile = fopen(inFileName, "r")) == NULL)
    /* opens file "inFileName" for reading */
    {
	    fprintf(stderr,"Error opening input file, %s\n", inFileName);
	    return -1;
    }

    *array = (char **)malloc((*capacity) * sizeof(char*));

    /* we only dereference once in order to pass-by-reference.
    ***array is a reference to an array of strings. The extra *
    is to denote that we are passing-by-reference.
    */

    if (*array == NULL)
    {
	    fprintf(stderr, "Malloc of array in loadArray failed!\n");
	    return -1;
    }

    printf("\nReading file '%s' (each . is 1000 words read)\n", inFileName);

    *count = 0; /*dereference the reference to count*/

    while (fscanf(inFile, "%s", word) == 1)
    {
	    if ((*count+1) >= *capacity)
	    {
        doubleArraySize(array, capacity);
	    }

	    if (insertWord(array, count, word, capacity) != 0)
	    {
	       fprintf(stderr,"    Insert returned an error!\n");
	       fclose(inFile);
	       return 1;
	    }
	
	    if (*count % 1000 == 0)
	    {
	      printf(".");
	      fflush(stdout);  /* stdout is buffered, so have to force flush */
	    }
    }

    fclose(inFile);

    return 0;
}
void IntCollection::add(int value)
{
    // Check to see if we need more space
    if (size == capacity)
    {
      doubleArraySize();
    }
    
    // Add the new value
    data[size] = value;
    size++;
}
void PointCollection::add(const point &p)
{
//	std::cout << "I'm in add, adding " << p.getX() << ", " << p.getY() << " to index " << size;
    // Check to see if we need more space
    if (size == capacity)
    {
        doubleArraySize();
    }
    
    // Add the new value
    data[size] = p;
    size++;

//    std::cout << " - added, size is now " << size << "\n";
}