class, array list, inserting from txt.file

Im tring to insert numbers from a txt file, search from a txt file and delete from a txt file.

I'm having trouble to read from a text file that has x amount of numbers store and inserting,searching and deleting them into a list.

I know how to read and display the numbers but i just can't figure out how to use it with the insert fucntion. i tried using

s1.Insert(numbers[count])

obvious that didnt work.

header:
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
#ifndef LIST_H
#define LIST_H

const int MAX_LENGTH = 60000;
typedef int ItemType;

class List
{
public:
	List();
	int Length() const;
	void Insert(ItemType item);
	void Delete(ItemType item);
	void SelSort();
               ItemType GetNextItem()
	bool IsPresent(ItemType item) const;
private:
	ItemType data[MAX_LENGTH];
	int length;
	int CurrentPos;
	void BinSerach(ItemType item, bool &FoundOrNot, unsigned int &Pos);

};

#endif 


implementation:
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
#include <iostream>
#include "list.h"

using namespace std;

List::List()
{
	length = 0;
}

int List::Length() const
{
	return length;
}

void List::Insert(ItemType item)
{
	data[length] = item;
	length++;
}

bool List::IsPresent(ItemType item) const
{
	int index = 0;
	while(index < length && item != data[index])
		index++;
	return (index < length);
}

void List::Delete(ItemType item)
{
	int index = 0;
	while (index < length && item != data[index])
		index++;
	if (index < length)
	{
		data[index] = data[length - 1];
		length--;
	}
}

ItemType List::GetNextItem()
{
	ItemType item;
	item = data[CurrentPos];
	if (CurrentPos == length -1)
		CurrentPos = 0;
	else
		CurrentPos ++;
	return item;
}


clint:
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
// This program reads data from a file into an array.
#include <iostream>
#include <fstream>
#include "list.h"
using namespace std;

int main()
{
	List s1;
	const int ARRAY_SIZE = 50000; 
	int numbers[ARRAY_SIZE];   
	int count = 0;		
	ifstream inputFile;        
	
	inputFile.open("c:/users/john_doe/documents/data.txt");

	// Read the numbers from the file into the array.
	while (count < ARRAY_SIZE && inputFile >> numbers[count])
       count++;
	
	
	inputFile.close();
	
	// Display numbers read:
	for (count = 0; count < ARRAY_SIZE; count++)
      cout << numbers[count] << endl;

	

	s1.Insert(numbers[count]);

               int l;
	l = s1.Length();
	for (int i = 0; i < l; i++)
		cout << "Item: " << s1.GetNextItem() << endl;
	
	return 0;
}
Last edited on
You have some crazy spacing going on. You're probably wanting to do something like:

1
2
3
4
5
for (count = 0; count < ARRAY_SIZE; count++)
{
      cout << numbers[count] << endl;
     s1.Insert(numbers[count]);
}


Brackets matter :)
sorry about the spacing, I just can't enough of it.

I tried that but what I'm trying to do is insert the numbers in a txt1 file (50000 numbers) into a list, search the numbers that are in txt2 file to see if there are on the list and remove/delete each of the numbers that are in txt3 file from the list.

I believe once i figure out out to insert the numbers in txt1 I can do the rest. But that is where I'm stuck, i can't figure how to insert the numbers in txt1 into the class array list. I love the challange but I've been added for a day and im shooting blanks lol.
Last edited on
So from what I can gather you have a text file that has a bunch of numbers. What you want to do is take each of these numbers and place them into some container, search the container for some set of numbers, if a match found, delete that from the file?

Ok let's break this into steps:
1) Need to start by knowing thow the numbers are seperated. Newlines, whitespaces, not separated at all but are all one digit?
2) Then you need to load the file
3) Now you need to iterate through this file pulling out each number (we know the format from step 1) and storing it in the container.
4) Does order matter? If not, sort the array real quick. If the order does, leave it alone.
5) Now search the container and remove all numbers that match a given set.
6) Output this container back to the file. You can just overwrite the old file now, though it's wise to tell it to save a backup of that file. On *nix systems, this is denoted with just "file_name.ext~", note the tilde.

To overwite a file, just open an ofstream binded to that file and use the ios::trunc flag.
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
The numbers are seperated by newlines and you're almost correct. Take the numbers from a text file 1 into a list, search the list for the numbers that are in file 2 and delete the numbers in the list that are in file 3. how about I post my assisngment to kind of understand what im trying to do.

I'm not worry about measuring the time and comparing because that is the least of my problem at the moment and I can figure that out on my own. but what i don't know is inserting the numbers in the file.

Implement unsorted AND sorted array-based list and compare the time it takes to perform the inserting operation, the searching operation, and the deletion operation respectively.

The numbers you should use for insertions are in file: hxxp://www.2shared.com/document/mtCXwPnv/prj2data.html

The numbers you should use for searching are in file: hxxp://www.2shared.com/document/8tH5LU_O/prj2search.html

The numbers you should use for deletions are in file: hxxp://www.2shared.com/document/Cqkd2ecn/prj2deldata.html

You should have the client code to insert each of the numbers of prj2data.txt into the list and measure the time. Then search each of the numbers of prj2search.txt to see whether it is in the list, and measure the time. And finally, remove each of the numbers of prj2deldata.txt from the list one by one and measure the time.

Submit the following: 1. The specification file and the implementation file for the two list classes; 2. The client code (should be the same for both list implementations); 3. The output screen shots showing the time of each operation, as well as the number of matching found during the search operation.

To measure the time, you can use the time( ) function, which give you the time in seconds at the exact moment when the function is called. Here is an example on the use of the function:

include ... time_t t_start, t_end, t_spent;

t_start = time (NULL); //do insertions... ... t_end = time(NULL); t_spent = t_end - t_start;
So, it wants you to delete any numbers that are in all three files?
Only delete the numbers that are found in file3 from the list, which list contains the numbers I inserted from file1.

ex:

file1:
1
2
3
4
5
8
9

insert each of the numbers of file1 into list

file 2:
1
4
7
8

search each of the numbers of file2 to see if it is in the list

file 3:
1
2
3
8
9

delete/remove each of the numbers of file 3 from the list
Last edited on
Topic archived. No new replies allowed.