Thread: reading data into a queue from file

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    reading data into a queue from file

    Hi everyone, I have a question. How do I open a file, and then read the data from the file into a queue (or some other temporary file structure that i can later transfer into a queue if reading into a queue is hard or undoable). The data in the file will look similar to:

    13 34.5034
    45 22.5543
    56 89.3333
    etc...

    The first value will represent something, and the second will represent something else. Also, is it possible to name the "processes" as they arrive into the queue or whatever type of file structure. For instance, to have the data in Field1 of Node1 contain "13" and Field2 of Node1 contain "34.5034", is it then possible to name that node that contains this information something like P1, and then have the next be called P2, P3, P4 and so on? So that way they can be referenced to later??? HELP!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes it's possible, but how you do it depends entirely on what you know and what therest of your code looks like. I would combine all the data into a struct/class. You can add extra member data to the struct/class like a name. You can then store instances of these objects in your queue.

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    I'm just looking for the simplest implementation to get the data from the file, into some type of temporary storage structure to later be rtransfered into some type of "Ready List" to simulate a Round Robin pre-emptive scheduler. I'm just having some trouble with it. Any suggestions other?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    use the fstream library to open the files and read in the data. and write a class or a struct to create data objects which you can store in any STL container or self written container you'd like.

    also, why would you bother with a temporary structure when it would be much more prudent to just put the information directly into the permanant storage structure.

  5. #5
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    i thought about that, but the simulator is going to grab data from that temporary storage unit as necessary. by putting all the data into a temporary storage area, i don't ahve to keep the file open and grab from it whenever i need.... i can just grab from the temp. i will have a dispatcher that grabs from that temp and places into a "ready list", thus telling the processor that it is ok to process the data. the first number in that list i listed above represents an "arrival time" and the second represents the "time it takes to process". so data will have to be gotten at different times, i'm guessing using a temp storage will eliminate me from having to keep the file open and continuously read from it... if i'm way off base here, just le me know...

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    I see, this can be done with just one data structure to store your input, but as with most programming problems there are a million different ways to do the same thing! You should feel free to do it whatever way you are comfortable.

    i'm guessing using a temp storage will eliminate me from having to keep the file open and continuously read from it
    the point of storing data is so that you don't have to keep the file open period, usually a temporary data structure is used to store data before it is processed and the result is store in another structure, I think that is what you were trying to say you are doing.

    anyway, the simplest implementation for you to get the data from the file would be this:
    Code:
    void read(){
    
    	ifstream buffer;
    	data_type tmp; //lets assume data_type is a struct with two members
    	vector<data_type> storage;
    
    	buffer.open("file_name.txt");
    
    	while(!buffer.eof()){
    
    		buff >> tmp.num1 >> tmp.num2;
    		storage.push_back(tmp);
    
    	}
    
    	buffer.close();
    	buffer.clear();
    
            return;
    
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You shouldn't use eof() to control the while loop in that way. It will often lead to a duplicate entry at the end of the storage vector. Better:
    Code:
    	while(buff >> tmp.num1 >> tmp.num2){
    
    		storage.push_back(tmp);
    	}

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    ahh thanks daved.....you actually just solved a huge mystery in a processing program i was working on, i was wondering why i always had to subtract 2 from the return of size() instead of one just 1 like the definition of my loops called for!

  9. #9
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    what #include must be added for "vector" to work?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    <vector> (and you must specify the std namespace).

  11. #11
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    it doesn't like the "vector" statement... here's the code I have so far... i'm a TERRIBLE c++ code writer, i design the projects, not code them.. so bare with me fella's....
    the object is the get the data from the inFile.txt and create a linked list with it and add at the tail and retrieve the data from the head (later on).

    Code:
    #include <string>
    #include <cstdlib>
    #include <iostream.h>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    class tempNode
    {
    
    public:
    	tempNode();
    
    	int arrivalTime;
    	float timeToProcess;
    	tempNode *link;
    
    };
    
    typedef tempNode nodePtr;
    
    tempNode::tempNode()
    {
    	arrivalTime = 0;
    	timeToProcess = 0;
    	link = NULL;
    }
    
    
    void getData();
    
    int main()
    {
    	int arrival = 0;
    	float timeProcess = 0.0;
    	
    
    	
    
    	return 0;
    
    }
    
    
    void getData()
    {
    	
    	nodePtr tempList;
    	ifstream inFile;
    
    	vector<nodePtr> storage;
    
    	//open the file
    	
    	inFile.open("inFile.txt.txt", ios::in);
    	// make sure file opened
    
    	if(!inFile)
    	{
    		cout<<"Error opening file!"<<endl;
    		exit (1);
    	}
    
    	while (inFile>>tempList.arrivalTime>>tempList.timeToProcess)
    	{
    
    		storage.push_back(tempList);
    	//	cout<<"arrival: "<<tempList.arrivalTime<<"  timeProcess: "<<tempList.timeToProcess<<endl;
    	}
    
    	inFile.close();
    	inFile.clear();
    
    	return;
    
    }

    thanks alot for all your help guys, you all are a life saver! my friend is sick who is my partner who writes the code after i design the project... unfortunately he is unable to do anything for like 4 weeks, so i'm left to forums!! thanks again...

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't #include <vector>. Also, it should be <iostream>, not <iostream.h>.

    However, your node class looks like it is set up to be a linked list. Chances are that you should be using that instead of vector (vector is something you would use instead of a linked list), especially since this is an assignment and you haven't been taught vector yet.

    So get rid of the vector stuff, and figure out how to use the Node to make a linked list.

  13. #13
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    Ok, i have it working so far... now the next step is to take the data from the node, and put it into a "ready list" according the first number in the text file: text file looks like...

    30 4.56667
    43 0.56674
    57 12.7786
    etc...

    where the first number represents the arrival time of the process to be sent from the tempList (used to store data from the file) into the "Ready List" and the second number represents how long for that process to stay in the loop iterations (faux CPU). My question is, how do I take the data and put it into the ready list now?

    heres the code so far...

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    
    class tempList
    {
    	public:
    		struct node;
    		typedef node *nodePtr;
    		struct node
    		{
    			int arrivalTime;
    			float timeToProcess;
    			nodePtr link;
    		};
    		tempList();
    		~tempList();
    		tempList(const tempList& L);
    
    		add(int inThing1, float inThing2);
    
    		void remove(int toBeRemoved);
    		void print();
    
    	private:
    		nodePtr head;
    };
    
    tempList::tempList()
    {
    	head = NULL;
    }
    
    tempList::~tempList()
    {
    	nodePtr p = head;
    	nodePtr q;
    	while(p)
    	{
    		q=p;
    		p=p->link;
    		delete(q);
    	}
    	head = NULL;
    }
    
    tempList::tempList(const tempList& L)
    {
    
    	nodePtr orig, copy, q;
    
    	if (L.head == NULL)
    		head = NULL;
    	else
    	{
    		head = new node;
    		head->link = NULL;
    		head->arrivalTime = L.head->arrivalTime;
    		head->timeToProcess = L.head->timeToProcess;
    		q = head;
    		orig = L.head->link;
    
    		while (orig!=NULL)  //more stuff to copy
    		{
    			copy = new node;
    			copy->arrivalTime = orig->arrivalTime;
    			copy->timeToProcess = orig->timeToProcess;
    			copy->link = NULL;
    			q->link = copy;
    
    			//advance pointers
    			orig = orig -> link;
    			q = copy;
    		}
    	}
    }
    
    void tempList::print()
    {
    	nodePtr walker;
    	walker = head;
    
    	while (walker != NULL)
    	{
    		cout<<walker->arrivalTime<<" "<<walker->timeToProcess<<endl;
    		walker = walker->link;
    	}
    }
    
    tempList::add(int inThing1, float inThing2)
    {
    	nodePtr p;
    
    	p = new node;    //allocates 
    	p->link=NULL;
    	p->arrivalTime = inThing1;  //fill the data
    	p->timeToProcess = inThing2;
    	p->link = head;   //links node into list as the first node
    	head = p;   //resets head
    }
    
    void tempList::remove(int toBeRemoved)    //removes from the back of the list... 
    {
    	nodePtr walker, shadow;
    	walker = head;
    	bool found = 0;
    
    	while ((walker != NULL) && !found)
    	{
    		if (walker->arrivalTime == toBeRemoved)
    			found = 1;
    		else  //advance pointers because value has not been found
    		{	
    			shadow = walker;
    			walker = walker->link;
    		}
    	}
    	if (walker == NULL)
    		cout<<"Not in list"<<endl;
    	else
    	{
    		shadow->link = walker->link;
    		delete (walker);
    	}
    }
    
    
    void getData();
    void processData();
    
    int main()
    {
    
    	
    	getData();
    	processData();
    
    	//now process the data and run simulation
    
    	
    	return 0;
    }
    
    void getData()
    {
    	tempList myList;
    	ifstream inFile;
    	int arrival;
    	float timeProcess;
    
    	inFile.open("times.txt", ios::out);
    
    	if (!inFile)
    	{
    		cout<<"Error opening file"<<endl;
    		exit(1);
    	}
    
    	while(inFile>>arrival>>timeProcess)
    	{
    		myList.add(arrival, timeProcess);
    
    	}
    	myList.print();
    }
    
    void processData()
    {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM