944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2760
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 2nd, 2005
0

opening files in classes

Expand Post »
excuse me as i am new to programming and trying to learn

i have searched through previous forum groups on this topic and i wish to seek the help of all tthe experts out there...
i wish to input a file into a class
i found this example
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. class job
  8. {
  9. string name;
  10. int length, time;
  11. string type;
  12. char urgent;
  13. public:
  14. bool read(string &line)
  15. {
  16. istringstream iss(line);
  17. return iss >> name >> length >> time >> type >> urgent;
  18. }
  19. void show()
  20. {
  21. cout << "name = " << name << '\n';
  22. cout << "length = " << length << '\n';
  23. cout << "time = " << time << '\n';
  24. cout << "type = " << type << '\n';
  25. cout << "urgent = " << urgent << '\n';
  26. cout << endl;
  27. }
  28. };
  29.  
  30. int main()
  31. {
  32. ifstream iFile("input1.txt");
  33. if ( iFile )
  34. {
  35. string line;
  36. while ( getline(iFile, line) )
  37. {
  38. job myjob;
  39. if ( myjob.read(line) )
  40. {
  41. myjob.show();
  42. }
  43. }
  44. }
  45. return 0;
  46. }


but it only works if the format looks like this
C++ Syntax (Toggle Plain Text)
  1. BAK 90001 200 TXT N
  2. CRSC 90010 150 TXT N
  3. MARY 90011 140 FP N
  4. CARL240 90050 300 FP N
  5. LOW236 90052 150 FP N

where the space is the next variable

how would i manipulate this code if my stucture was on a per line basis as opposed to a space basis for the variable?
e.g.
C++ Syntax (Toggle Plain Text)
  1. bak
  2. 90001
  3. 200
  4. txt
  5. n
  6. CRSC
  7. 90010
  8. 150
  9. TXT
  10. N

i need it to read the WHOLE line... as the name may or may not have a first and/or last name


thanks in adavance
Last edited by Dave Sinkula; Oct 2nd, 2005 at 11:25 pm. Reason: Added [code][/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 2nd, 2005
0

Re: opening files in classes

understand what the code is doing. In main you make an input file and read it a line at a time. You then pass that line to myjob::read. This is where the parsing occurs. first an istringstream is constructed from our line of input then several items are extracted from it. This works the same as extracting items from cin but instead of a keyboard for input it reads from a string. Now have a go at solving your problem. How would you do it if you was reading from cin. you do the same with a stringstream.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 3rd, 2005
0

Re: opening files in classes

Quote originally posted by Stoned_coder ...
understand what the code is doing. In main you make an input file and read it a line at a time. You then pass that line to myjob::read. This is where the parsing occurs. first an istringstream is constructed from our line of input then several items are extracted from it. This works the same as extracting items from cin but instead of a keyboard for input it reads from a string. Now have a go at solving your problem. How would you do it if you was reading from cin. you do the same with a stringstream.
like i said i am very ver new to this...
but that makes logical sense
if i was reading from cin...
i would go something like
iss.cin << name << length << time << type << urgent;
is this correct....?
would i be right in therefore thinking ...
iss.istringstream << name << length << time << type << urgent;

but how would i get each line to read to say about 5 lines per class?


btw thanks for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 3rd, 2005
0

Re: opening files in classes

Minor tweaks:
#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(ifstream &file)
   {
      return file >> 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("file.txt");
   if ( iFile )
   {
      job myjob;
      while ( myjob.read(iFile) )
      {
         myjob.show();
      }
   }
   return 0;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 4th, 2005
0

Re: opening files in classes

mate your a legend... it was frustrating me something severe..!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 4th, 2005
0

Re: opening files in classes

Quote originally posted by Dave Sinkula ...
Minor tweaks:
#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(ifstream &file)
   {
      return file >> 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("file.txt");
   if ( iFile )
   {
      job myjob;
      while ( myjob.read(iFile) )
      {
         myjob.show();
      }
   }
   return 0;
}

sorry one more question...
maybe i didnt make myself clear eariler...
if i have more then one record.... say about 10 records for the entire file

and want to be able to manipluate them individually... the code above only lets me edit the final class ....
e.g. aka myjob
if wanted to say declare 10 records whats the best way to do it
and how would that implement
e.g.

]#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(ifstream &file)
{
return file >> 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("file.txt");
if ( iFile )
{
job myjob, myjob1, myjob2, myjob3, myjob4; //..etc;
while ( myjob.read(iFile) )
{

}
while ( myjob1.read(iFile) ) //etc...
{

}
myjob.show();
myjob1.show();
myjob2.show();
myjob3.show(); //etc...
}
return 0;


like i said eariler what is a good way to code this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 4th, 2005
0

Re: opening files in classes

Quote originally posted by bobobobo51 ...
if i have more then one record.... say about 10 records for the entire file

and want to be able to manipluate them individually... the code above only lets me edit the final class ....
e.g. aka myjob
if wanted to say declare 10 records whats the best way to do it
and how would that implement
You're pretty much hinting that you want an array of 10. First, let me make this change (so I can be really lazy later on).
class job
{
   string  name;
   int     length, time;
   string  type;
   char    urgent;
public:
   bool read(ifstream &file)
   {
      return file >> name >> length >> time >> type >> urgent;
   }
   friend ostream& operator<< (ostream &o, const job &j);
};

ostream& operator<< (ostream &o, const job &j)
{
   o << "name   = " << j.name   << '\n';
   o << "length = " << j.length << '\n';
   o << "time   = " << j.time   << '\n';
   o << "type   = " << j.type   << '\n';
   o << "urgent = " << j.urgent << '\n';
   o << endl;
   return o;
}
Then after you've opened the file, you might have something like this.
C++ Syntax (Toggle Plain Text)
  1. job myjob[10];
  2. for ( size_t i = 0; i < sizeof myjob / sizeof *myjob; ++i )
  3. {
  4. if ( !myjob[i].read(iFile) )
  5. {
  6. break;
  7. }
  8. cout << myjob[i];
  9. }
But if you really don't want to hard-code a size of 10, I'd say use a vector.

Some additional headers are necessary for this example:
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iterator>
Now after the file is opened, code to read and display it might be as follows.
C++ Syntax (Toggle Plain Text)
  1. job ajob;
  2. vector<job> myjob;
  3. while ( ajob.read(iFile) )
  4. {
  5. myjob.push_back(ajob);
  6. }
  7. copy(myjob.begin(), myjob.end(), ostream_iterator<job>(cout, "\n"));
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 4th, 2005
0

Re: opening files in classes

thanks mate ive learnt sooo much in the past couple days...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 5th, 2005
0

Re: opening files in classes

ok... is it okay if i you help me expand my knowledge even more?
take a different example...
say i got a file of swimmers

e.g.
C++ Syntax (Toggle Plain Text)
  1. M
  2. 100 Free
  3. 50.24
  4. Ian Thorpe
  5. 10/12/1960
  6. 10/02/2005
  7. Wagga Wagga
  8. 1
  9. M
  10. 100 Free
  11. 51.01
  12. Grant Hackett
  13. 20/07/69
  14. 12/03/2005
  15. Melbourne
  16. 2
  17. M
  18. 100 Free
  19. 52.00
  20. John Konrads
  21. 22/10/1970
  22. 14/04/2005
  23. Adelaide
  24. 3

where the lines read
C++ Syntax (Toggle Plain Text)
  1. Sex M
  2. Event eg 100 Free
  3. Performance 50.24
  4. Name Ian Thorpe
  5. Date of birth 10/12/1960
  6. Data of competition 10/2/2005
  7. Venue Wagga Wagga
  8. Position 1

lets make it interesting and say... we can put those files into a struct ...
e.g.
C++ Syntax (Toggle Plain Text)
  1. #include <iomanip>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main(){
  9. fstream fs;
  10. int const MAX = 11;
  11. struct swim
  12. {
  13. char sex[2];
  14. char eventtype[20];
  15. char perf[20];
  16. char name[20];
  17. char dob[11];
  18. char doc[11];
  19. char venue[20];
  20. char position[2];
  21. };
  22.  
  23. swim record[MAX];
  24.  
  25. int count=0;
  26. fs.open("c:\\swimmers.txt", ios::in);
  27. if(fs.fail()) return 2;
  28.  
  29. while (!fs.eof() && count < MAX)
  30. {
  31.  
  32. fs.getline(record[count].sex,2,'\n');
  33. for(int i=0; i<1;i++)
  34. fs.getline(record[count].eventtype,20,'\n');
  35. for(int i=0; i<1;i++)
  36. fs.getline(record[count].perf,20,'\n');
  37. for(int i=0; i<1;i++)
  38. fs.getline(record[count].name,21,'\n');
  39. for(int i=0; i<1;i++)
  40. fs.getline(record[count].doc,11,'\n');
  41. for(int i=0; i<1;i++)
  42. fs.getline(record[count].dob,11,'\n');
  43. for(int i=0; i<1;i++)
  44. fs.getline(record[count].venue,21,'\n');
  45. for(int i=0; i<1;i++)
  46. fs.getline(record[count].position,2,'\n');
  47. for(int i=0; i<1;i++)
  48.  
  49. cout << record[count].sex << "\n";
  50. cout << record[count].eventtype << "\n";
  51. cout << record[count].perf << "\n";
  52. cout << record[count].name << "\n";
  53. cout << record[count].doc << "\n";
  54. cout << record[count].dob << "\n";
  55. cout << record[count].venue << "\n";
  56. cout << record[count].position << "\n";
  57.  
  58. count++;
  59.  
  60. fs.close();
  61. cout << record[6].name;
  62.  
  63. system("pause");
  64. return 0;
  65. }
<< moderator edit: added code tags: [code][/code] >>

and i wanna make classes BASED on the events e.g.
Men or women denoted by M or W.The events are 100m Free, 200m Back, 400m Medley, 800m Back, 1500m Free.
therefore in total 10 classes...
and inside those classes i got a struct of each swimmer with the apprioate event
i was thinking something along the lines of

C++ Syntax (Toggle Plain Text)
  1. struct athlete{
  2. name
  3. performance
  4. dob
  5. doc
  6. venue
  7. position
  8. }
  9.  
  10.  
  11. struct record{
  12. sex
  13. event
  14. name
  15. performance
  16. dob
  17. doc
  18. venue
  19. position
  20. }
  21. const int MAXSIZE = 10;
  22. class rank
  23. {
  24. string sex, event;
  25. athlete data[maxsize];
  26. int total;
  27. public
  28. rank (string &s, string &e);
  29. string getsex();
  30. string getevent();
  31. int gettotal();
  32. althelete 7getdata(int i );
  33. void sort();
  34. void add();
  35. void replace(int, athlete&);
  36. void remove(int position);
  37. friend ostream &operator<<(ostream&, const rank&):
  38. void save(ostream&);
  39. };
so a user is prompted with an option of selecting 1) to select the event of the class.... e.g. 100 free male
when selected given the option to edit it
therefore when an event is selected in option 1 the appropriate object should be selected. Option 2 will involve searching the list of swimmers names in the object

whats a good way of approching this??
i was thinking develop a class and functions similar to the above...
with the apprioprate structs....
am i on the right track? any thoughts?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobobobo51 is offline Offline
9 posts
since Oct 2005
Oct 6th, 2005
0

Re: opening files in classes

Ah, if only I had more time to spend...

First thing I'd recommend is using std::string [note to Dani: here again is where I'd prefer a fixed font] instead of a char array; perhaps numeric fields where appropriate. Put them in your class or struct -- let's call it a record. Taking input for a record could a number of lines, similar to previous posts. And again use use::vector. Something like that. And there's this.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Guessing Game
Next Thread in C++ Forum Timeline: Symbian help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC