Hello evreryone,

I want to start by saying I did use the search function and I did read the fstream tutorial. But I didn't see the answer I need.

I have been given a text file for which I need to read input from. Here is a partial listing of the file...

BAK 90001 200 TXT N
CRSC 90010 150 TXT N
MARY 90011 140 FP N
CARL240 90050 300 FP N
LOW236 90052 150 FP N

The file contains four columns which need to be put into different members of the class. For example, how can I read the first line and put "BAK" into member0, "90001" into member1, "200" into member2, "TXT" into member3 and "N" into member four?

Also, after reading the first line, I need to stop reading the file to perform some functions then continue reading the file on the next line. This repeats until all the lines are read.

Any help is greatly appreciated! :confused:

Recommended Answers

All 9 Replies

I want to start by saying I did use the search function and I did read the fstream tutorial. But I didn't see the answer I need. :confused:

Show us the code from your initial attempt and describe the shortcomings.

[QUOTE=Dave Sinkula]Show us the code from your initial attempt and describe the shortcomings.[/QUOTE]

OK, but please don't laugh. I am not good at coding so I'm sure you'll find plenty of mistakes. BTW, I am also having a problem creating objects of my job class using templates (that's why it's commented out).

My assignment is to put these jobs (each line of the input file) into a queue based on its type; mm, fp or txt. So I will need 3 queues.

So my idea was to read each job or line from the file, place it in a job object called "buff", determine its type, place it in its appropriate job object (mm, fp or txt) and read the next line into the "buff" object until all the lines have been read and sorted into their approriate objects.

Thanks for looking!

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
template <class ItemType>
class job 
{
public: 
    job()
    {
        ItemType switches=0;
        ItemType fTime=0;
        ItemType urgent=0;
    }    
    job::~job()
    {
    } 

    job<ItemType>* next;
    job<ItemType>* back;

    void setName(ItemType name);
    ItemType getName();
    void setLength(ItemType length);
    void decLength(ItemType length);
    ItemType getLength();
    void setAtime (ItemType time);
    ItemType getAtime();
    void setStime (ItemType sTime);
    ItemType getStime();
    void setFtime (ItemType fTime);
    ItemType getFtime();
    void incSwtch (ItemType switches);
    ItemType getSwitch();
    void setType (ItemType type);
    ItemType getType();
    void setBool (ItemType urgent);
    ItemType getBool();
private:
    ItemType _name;
    ItemType _length;
    ItemType _aTime;
    ItemType _sTime;
    ItemType _fTime;
    ItemType _switches;
    ItemType _type;
    ItemType _urgent;
};

int main()
{
    /*
    job<ItemType> buff;
    job<ItemType> mm;
    job<ItemType> txt;
    job<ItemType> fp;
    */
    ifstream iFile("d:/input1.txt");

      if (! iFile)
    {
        cout << "Error opening input file" << endl;
        return -1;
    }
    cout << "File opened" << endl;

    while(!iFile.eof())
   {
        getline(iFile, buff.name, ' ');
        getline(iFile, buff.length, ' ');
        getline(iFile, buff.aTime, ' ');
        getline(iFile, buff.type, ' ');
        getline(iFile, buff.urgent, ' ');
    }
    iFile.close();


template<class ItemType>
    void job<ItemType>::setName(ItemType name)
    {
        _name=name;
    } 
template<class ItemType>  
    ItemType job<ItemType>::getName()
    {
        return _name;
    }
template<class ItemType>    
    void job<ItemType>::setLength(ItemType length)
    {
        _length=length;
    }
template<class ItemType>    
    void job<ItemType>::decLength(ItemType length)
    {
        _length=_length-50;
    }
template<class ItemType>    
    ItemType job<ItemType>::getLength()
    {
        return _length;
    }
template<class ItemType>    
    void job<ItemType>::setAtime (ItemType aTime)
    {
        _aTime=aTime;
    }
template<class ItemType>    
    ItemType job<ItemType>::getAtime()
    {
        return _aTime;
    }    
template<class ItemType>
    void job<ItemType>::setStime (ItemType sTime)
    {
        _sTime=sTime;
    }    
template<class ItemType>
    ItemType job<ItemType>::getStime()
    {
        return _sTime;
    }    
template<class ItemType>
    void job<ItemType>::setFtime (ItemType fTime)
    {
        _fTime=fTime;
    }    
template<class ItemType>
    ItemType job<ItemType>::getFtime()
    {
        return _fTime;
    }    
template<class ItemType>
    void job<ItemType>::incSwtch (ItemType switches)
    {
        _switches++;
    }
template<class ItemType>    
    ItemType job<ItemType>::getSwitch()
    {
        return _switches;
    }    
template<class ItemType>
    void job<ItemType>::setType (ItemType type)
    {
        _type=type;
    }    
template<class ItemType>
    ItemType job<ItemType>::getType()
    {
        return _type;
    }    
template<class ItemType>
    void job<ItemType>::setBool (ItemType urgent)
    {
        _urgent=urgent;
    }    
template<class ItemType>
    ItemType job<ItemType>::getBool()
    {
        return _urgent;
    }

Ok, I think I'm screwing things up big time trying to use templates, apparently incorrectly. Now I can create my job objects that I need but I am still having problems reading from the file. Help?

So here is another version without templates...

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class job 
{
public: 
    job()
    {
        int switches=0;
        int fTime=0;
        int urgent=0;
    }    
    job::~job()
    {
    } 

    job* next;
    job* back;

    void setName(string name);
    string getName();
    void setLength(int length);
    void decLength(int length);
    int getLength();
    void setAtime (int time);
    int getAtime();
    void setStime (int sTime);
    int getStime();
    void setFtime (int fTime);
    int getFtime();
    void incSwtch (int switches);
    int getSwitch();
    void setType (string type);
    string getType();
    void setBool (bool urgent);
    bool getBool();
private:
    string _name;
    int _length;
    int _aTime;
    int _sTime;
    int _fTime;
    int _switches;
    string _type;
    bool _urgent;
};

int main()
{

    job buff;
    job mm;
    job txt;
    job fp;

    ifstream iFile("d:/input1.txt");

      if (! iFile)
    {
        cout << "Error opening input file" << endl;
        return -1;
    }
    cout << "File opened" << endl;

    while(!iFile.eof())
   {
        getline(iFile, buff.setName, ' ');
        getline(iFile, buff.setLength, ' ');
        getline(iFile, buff.setAtime, ' ');
        getline(iFile, buff.setType, ' ');
        getline(iFile, buff.setBool, ' ');
    }
    iFile.close();
}

void job::setName(string name)
    {
        _name=name;
    } 
string job::getName()
    {
        return _name;
    }
void job::setLength(int length)
    {
        _length=length;
    }
void job::decLength(int length)
    {
        _length=_length-50;
    }
int job::getLength()
    {
        return _length;
    }
void job::setAtime (int aTime)
    {
        _aTime=aTime;
    }
int job::getAtime()
    {
        return _aTime;
    }    
void job::setStime (int sTime)
    {
        _sTime=sTime;
    }    
int job::getStime()
    {
        return _sTime;
    }    
void job::setFtime (int fTime)
    {
        _fTime=fTime;
    }    
int job::getFtime()
    {
        return _fTime;
    }    
void job::incSwtch (int switches)
    {
        _switches++;
    }
int job::getSwitch()
    {
        return _switches;
    }    
void job::setType (string type)
    {
        _type=type;
    }    
string job::getType()
    {
        return _type;
    }    
void job::setBool (bool urgent)
    {
        _urgent=urgent;
    }    
bool job::getBool()
    {
        return _urgent;
    }

Hmm. Here is what I had been thinking.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
   ifstream iFile("input1.txt");
   if ( iFile )
   {
      string line;
      while ( getline(iFile, line) )
      {
         istringstream iss(line);
         string  name, type;
         int     length, time, switches;
         char    urgent;
         if ( iss >> name >> length >> time >> type >> urgent )
         {
            cout << "name   = " << name   << '\n';
            cout << "length = " << length << '\n';
            cout << "time   = " << time   << '\n';
            cout << "type   = " << type   << '\n';
            cout << "urgent = " << urgent << '\n';
            cout << endl;
         }
      }
   }
   return 0;
}

/* input1.txt
BAK 90001 200 TXT N
CRSC 90010 150 TXT N
MARY 90011 140 FP N
CARL240 90050 300 FP N
LOW236 90052 150 FP N
*/

/* my output
name   = BAK
length = 90001
time   = 200
type   = TXT
urgent = N

name   = CRSC
length = 90010
time   = 150
type   = TXT
urgent = N

name   = MARY
length = 90011
time   = 140
type   = FP
urgent = N

name   = CARL240
length = 90050
time   = 300
type   = FP
urgent = N

name   = LOW236
length = 90052
time   = 150
type   = FP
urgent = N
*/

[edit]And then I might adapt that to a class something like this.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

class job
{
   string  name;
   int     length, time;
   string  type;
   char    urgent;
public:
   bool read(string &line)
   {
      istringstream iss(line);
      return iss >> name >> length >> time >> type >> urgent;
   }
   void show()
   {
      cout << "name   = " << name   << '\n';
      cout << "length = " << length << '\n';
      cout << "time   = " << time   << '\n';
      cout << "type   = " << type   << '\n';
      cout << "urgent = " << urgent << '\n';
      cout << endl;
   }
};

int main()
{
   ifstream iFile("input1.txt");
   if ( iFile )
   {
      string line;
      while ( getline(iFile, line) )
      {
         job myjob;
         if ( myjob.read(line) )
         {
            myjob.show();
         }
      }
   }
   return 0;
}

Thank you for all your help so far. I took what you gave me and modified it so I can place each line into a new object.

I feel as though my code can be improved upon since I am declaring a known amount of objects (since I can see the contents of the input file). It seems like cheating to me. Can anyone think of a way to somehow loop the input into new objects until there are no more lines to read?

I tried...

int i=0;
getline(iFile, line);     
      myjob[i].read(line);
      cout<<"myjob "<<i<<endl;
      myjob[i].show();

But the compiler doesn't like myjob[i]. It says it is undeclared. I am not allowed to use arrays by the way and it probably thinks it is an array. I am using objects declared (or trying to be declared as) myjob0, myjob1, myjob2, etc.

Anyhow, I coded for the first 5 objects and lines to be read from the file. Before I make a big ugly mess of something that is probably much simpler to do, can someone give me a clue in the right direction?

Below is what I have so far.

Thanks a bunch I am learning a lot of C++ today!!!

:cool:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

class job
{
   string  name;
   int     length, time;
   string  type;
   char    urgent;
public:

   bool read(string &line)
   {
      istringstream iss(line);
      return iss >> name >> length >> time >> type >> urgent;
   }
   void show()
   {
      cout << "name   = " << name   << '\n';
      cout << "length = " << length << '\n';
      cout << "time   = " << time   << '\n';
      cout << "type   = " << type   << '\n';
      cout << "urgent = " << urgent << '\n';
      cout << endl;
   }
};

int main()
{
   ifstream iFile("input1.txt");
   if ( iFile )
   {
      job myjob0;   //my input file has 32 lines!!!  so I need 32
      job myjob1;  //myjob declarations and 32 getline thingamabobs
      job myjob2;  //Yikes!!  It's gonna look ugly.  There has to be a 
      job myjob3;  //better way!
      job myjob4;
      job myjob5;            
      string line;

      getline(iFile, line);     
      myjob0.read(line);
      cout<<"myjob0"<<endl;
      myjob0.show();

      getline(iFile, line);     
      myjob1.read(line);
      cout<<"myjob1"<<endl;
      myjob1.show();

      getline(iFile, line);     
      myjob2.read(line);
      cout<<"myjob2"<<endl;
      myjob2.show();

      getline(iFile, line);     
      myjob3.read(line);
      cout<<"myjob3"<<endl;
      myjob3.show();     

      getline(iFile, line);     
      myjob4.read(line);
      cout<<"myjob4"<<endl;
      myjob4.show();

      getline(iFile, line);     
      myjob5.read(line);
      cout<<"myjob5"<<endl;
      myjob5.show();



      }
   return 0;
}

Thank you for all your help so far. I took what you gave me and modified it so I can place each line into a new object.

I feel as though my code can be improved upon since I am declaring a known amount of objects (since I can see the contents of the input file). It seems like cheating to me. Can anyone think of a way to somehow loop the input into new objects until there are no more lines to read?

From your earlier posting of the class, it looks like a linked list was at least implied.

job* next;
job* back;

From your earlier posting of the class, it looks like a linked list was at least implied.

job* next;
job* back;

Yes, each line of the input file is a job which we have to process and then place into one of three doubly linked lists (as a queue) according to the job's type (either mm, fp or txt),

We are trying to simulate a priority alogrithm of an operating system. And as such as we read each job (line) we process it by decreasing its job length and place it into the appropriate queue. Then we increase the system time and process the next job and so on.

But at some point the system time will be greater than the arrival time of the new job so the job won't be processed ( don't decrease it's job length), it will just be put into the queue.

Then just to make it fun, some jobs have a priority to them. It's the last bit of info on the line, either Y or N. If it is Y, then the job has to bump up to the front of the queue.

Oh boy I love this stuff!

i know this forum link hasnt been looked at in a while.. but on the previous example how would you read the file if the contents was different
say the separator was a new line \n
therefore one record would be

BAK
90001
200
TXT
N
CRSC
90010
150
TXT
N

from this....

MARY        90011   140 FP  N
CARL240     90050   300 FP  N
LOW236      90052   150 FP  N

your input would be much appreciated

thanks in advance

currently the space is the separator for the record into the class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.