コード例 #1
0
ファイル: clab.c プロジェクト: klopez9/dictionary
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;
}
コード例 #2
0
ファイル: IntCollection.cpp プロジェクト: DemingYan/110B_work
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++;
}
コード例 #3
0
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";
}