| | |
opening files in classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 9
Reputation:
Solved Threads: 0
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
but it only works if the format looks like this
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.
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
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)
#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; }
but it only works if the format looks like this
C++ Syntax (Toggle Plain Text)
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
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)
bak 90001 200 txt n CRSC 90010 150 TXT 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.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
•
•
Join Date: Oct 2005
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
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.
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
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;
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2005
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
•
•
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
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;
} C++ Syntax (Toggle Plain Text)
job myjob[10]; for ( size_t i = 0; i < sizeof myjob / sizeof *myjob; ++i ) { if ( !myjob[i].read(iFile) ) { break; } cout << myjob[i]; }
Some additional headers are necessary for this example:
C++ Syntax (Toggle Plain Text)
#include <vector> #include <algorithm> #include <iterator>
C++ Syntax (Toggle Plain Text)
job ajob; vector<job> myjob; while ( ajob.read(iFile) ) { myjob.push_back(ajob); } copy(myjob.begin(), myjob.end(), ostream_iterator<job>(cout, "\n"));
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2005
Posts: 9
Reputation:
Solved Threads: 0
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.
where the lines read
lets make it interesting and say... we can put those files into a struct ...
e.g.
<< 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
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?
take a different example...
say i got a file of swimmers
e.g.
C++ Syntax (Toggle Plain Text)
M 100 Free 50.24 Ian Thorpe 10/12/1960 10/02/2005 Wagga Wagga 1 M 100 Free 51.01 Grant Hackett 20/07/69 12/03/2005 Melbourne 2 M 100 Free 52.00 John Konrads 22/10/1970 14/04/2005 Adelaide 3
where the lines read
C++ Syntax (Toggle Plain Text)
Sex M Event eg 100 Free Performance 50.24 Name Ian Thorpe Date of birth 10/12/1960 Data of competition 10/2/2005 Venue Wagga Wagga Position 1
lets make it interesting and say... we can put those files into a struct ...
e.g.
C++ Syntax (Toggle Plain Text)
#include <iomanip> #include <fstream> #include <iostream> using namespace std; int main(){ fstream fs; int const MAX = 11; struct swim { char sex[2]; char eventtype[20]; char perf[20]; char name[20]; char dob[11]; char doc[11]; char venue[20]; char position[2]; }; swim record[MAX]; int count=0; fs.open("c:\\swimmers.txt", ios::in); if(fs.fail()) return 2; while (!fs.eof() && count < MAX) { fs.getline(record[count].sex,2,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].eventtype,20,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].perf,20,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].name,21,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].doc,11,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].dob,11,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].venue,21,'\n'); for(int i=0; i<1;i++) fs.getline(record[count].position,2,'\n'); for(int i=0; i<1;i++) cout << record[count].sex << "\n"; cout << record[count].eventtype << "\n"; cout << record[count].perf << "\n"; cout << record[count].name << "\n"; cout << record[count].doc << "\n"; cout << record[count].dob << "\n"; cout << record[count].venue << "\n"; cout << record[count].position << "\n"; count++; fs.close(); cout << record[6].name; system("pause"); return 0; }
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)
struct athlete{ name performance dob doc venue position } struct record{ sex event name performance dob doc venue position } const int MAXSIZE = 10; class rank { string sex, event; athlete data[maxsize]; int total; public rank (string &s, string &e); string getsex(); string getevent(); int gettotal(); althelete 7getdata(int i ); void sort(); void add(); void replace(int, athlete&); void remove(int position); friend ostream &operator<<(ostream&, const rank&): void save(ostream&); };
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?
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.
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Trouble opening certain files... (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Guessing Game
- Next Thread: Symbian help
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






