(file) Return to acm.cpp CVS log (file) (dir) Up to [RizwankCVS] / acm

 1 rizwank 1.1 #include<stdlib.h>
 2             #include<iostream>
 3             #include<fstream>
 4             #include<string>
 5             using namespace std;
 6             
 7             // have to take data from command line
 8             // have to deal with non int #s
 9             
10             int intcomp (const void * a, const void * b)
11             {
12               return ( *(int*)a - *(int*)b );
13             }
14             
15             
16             
17             
18             int main(int argc, char *argv[])
19             {
20             	int max_inventory;
21             	
22 rizwank 1.1 	ifstream inputfs;
23             	inputfs.open ("data.txt");
24             	if (!inputfs.is_open())
25             	  { printf("File open failed!\n"); 
26             		return 1; }
27             	printf("File Open!\n");
28             	int length;
29             	inputfs.seekg (0, ios::end);
30             	length = inputfs.tellg();
31             	inputfs.seekg (0, ios::beg);
32             	
33             	max_inventory= 1 + length/2;
34             	// Elements cannot be more than this amount
35             	
36             	char * read_buffer = (char *) malloc (length);
37             	int * inventory = (int * ) malloc (length);
38             	inputfs.getline (read_buffer, length-1);
39             	
40             
41             	char * theToken; int index = 0;
42             	theToken=strtok(read_buffer,",");
43 rizwank 1.1 	// assume 1+ items
44             	while ( theToken != NULL ) {
45             		inventory[index] = atoi(theToken);
46             		printf("index %d, token %s, value %d\n",index,theToken,inventory[index]);
47             		
48             		index++;
49             		theToken = strtok (NULL,",");
50             		}
51             	printf("File Opened!\n");
52             	// still have to read total size and close file
53             
54             	int size_inventory = index; //remember this is 1 based, not 0
55             	
56             		
57             	printf ( "Read %d elements!\n",size_inventory);
58             	qsort ( inventory, size_inventory, sizeof(int), intcomp);
59             	
60             	// we now have a sorted list
61             	
62             
63             	
64 rizwank 1.1 	
65             	// ckeanup, release memeory, close files here		
66             	return 0;
67             }

Rizwan Kassim
Powered by
ViewCVS 0.9.2