calling a appending function from a read file function

I need to add a method readFile() which will read integer values from a file named as numbers.txt. The method will open, check and close numbers.txt. The integers are put in the list using the appendToList() method. Read the file one integer at a time; if there are too many integers for the size of the array, simply ignore the extras. The code appendToList was given to me and I created the readFile(). It's not appending it and I don't know if I'm calling it wrong or what

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
ItemList::ItemList(int arraySize = 10)
{
    // dynamically allocate the list array
    listItem = new int[arraySize];
	maxSize = arraySize;
	// be sure to note that listItem[0] is undefined at this point

	// set all node links to indicate node available for insert
	clearList();

}

bool ItemList::appendToList(int value)
{
    bool itWorked = false;	// safest default is insert failed

	if ( isEmpty() ) // this will be the first item
	{
        // this "wakes up" all the list pointers
        topItem = listItem;
        currentItem = listItem;
        nextAvailable = currentItem + 1;

        *currentItem = value;

        itWorked = true;
    }
    else if ( (nextAvailable - listItem) < maxSize ) // there is space available
	{
        currentItem = nextAvailable;
        ++nextAvailable;
        *currentItem = value;

        itWorked = true;
	} // there is space available
	else
	{
        itWorked = false;
    }
	return itWorked;
}

void ItemList::readFile()
{
	ifstream inFile; 
	inFile.open("numbers.txt"); //open the file numbers.txt

	if(!inFile)//if file doesn't open
	{
		cout << "Could not open file!" << endl;
	}
	else
	{
		int i = 0;
		while(inFile >> listItem[i])//reads the ints int the array of listItem
		{
			appendToList(i); //appends to lsit
			i++;

		}
	}
	inFile.close();

}
Last edited on
Topic archived. No new replies allowed.