![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Then why not format the file differently in the first place. i.e format the file in for easy programmatic extraction, then make it look pretty on the command line.
|
|
|
|
|
|
#12 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
What do you mean?
|
|
|
|
|
|
#13 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You know what the headers are - you're writing the program. So embed them into your program. Then format the data like this:
6162 F 12.23 1835 M 11.75 8320 F 11.80 |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The point about reformatting your file is so germane, I'd like to emphasize it. Data should be stored in an organized way that's easy to retrieve. Formatting is for human eyeballs. If your data were formatted properly, you could present it in numerous ways. One way might be as shown in your post. Maybe you'd rather present it with the wages converted to an annual salary. You could do that with the same information. Think of querying a database and formatting the results however you like, rearranging or discarding certain data according to your needs.
That said, the file as shown COULD be successfully parsed. I doubt it's really stored as shown, with all the whitespace, though. I would hope not. Terrific waste. I'll give it marks for "highly compressible", though.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Please forgive me for being stupid here, but I'm still not following. I'm brand new to writing and reading files, and iostream operators, and fstream, ifstream, blah blah, all brand new. If by headers you mean the labeling, they are in the program, embedded at least. I'm not quite sure what you mean by formatting though. If you mean writing them to the file as you've shown, it makes sense, but I understood the question as in printing directly what the user sees to a file, which i did, not in an easily retrievable form. If that were the case, i'd be very very happy. But, from the form in which i have it, why wouldn't getline retrieve it line by line in the first place? If not getline, why not "<<" like i have it now? I'm not trying to be difficult, I just like to know why things don't work and how I can avoid them in the future. It's all a learning experience!!!
|
|
|
|
|
|
#16 | |||
|
Professional Programmer
|
It's quite hard not to understand what everyone's been talking about, you must be trying pretty hard.
When a file is saved, you might end up with a file which has 4 saved integers, like so: Quote:
Quote:
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|||
|
|
|
|
|
#17 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
i totally understand now...lol...i'm a hardass (and head)!
|
|
|
|
|
|
#18 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Now that you won't do it, here's how
(string streams are nice, too)#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class bastardRecord
{
public:
string ID;
string gender;
double wage;
double longevity;
};
istream& operator>>(istream& is, bastardRecord& rec)
{
string dummy;
is >> rec.ID;
is >> rec.gender;
is >> dummy;
is >> rec.wage;
is >> rec.longevity;
return is;
}
int OMGBBQ111elevenHashbang (string catAssTrophy)
{
cerr << catAssTrophy << endl;
return -1;
}
int main (int argc, char *argv [])
{
/*
Employee ID Employee Sex Numeric Wage Years with Company
----------- ------------ ------------ ------------------
6162 F $ 12.23 355
4654654 M $ 23.60 6
*/
string header, separator, dummy;
bastardRecord theData;
vector <bastardRecord> document;
ifstream inFile;
inFile.open ("input.txt", ios::in | ios::binary);
if (!inFile.good ()) return OMGBBQ111elevenHashbang ("Failed to open input file");
cout << "The input file: \n\n" << endl;
cout << inFile.rdbuf ();
// Position back to the beginning
inFile.seekg (0, ios_base::beg);
// Blow off header information
getline (inFile, header);
getline (inFile, separator);
if (inFile.fail ())return OMGBBQ111elevenHashbang ("File too short");
// Read the remainder
while (true)
{
inFile >> theData;
if (inFile.fail ())
if (!inFile.eof ()) return OMGBBQ111elevenHashbang ("File input failure");
else break;
document.push_back (theData);
}
cout << "The data:\n\n";
for (int i = 0; i < document.size (); i++)
{
cout << "ID: " << document [i].ID << ", "
<< "Gender: " << document [i].gender << ", "
<< "Wage: " << document [i].wage << ", "
<< "Longevity: " << document [i].longevity << endl;
}
cout << endl;
cout << "Press ENTER to terminate program" << endl;
cin.get ();
return 0;
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#19 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
i'm definitely not quite that clever with vectors or classes...especially classes!
|
|
|
|
|
|
#20 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A class is just a struct. A class is just a struct. A container. It may contain different types inside. Neato. It can also contain methods for manipulating them. If that's too much, defer it. But don't toss away the ability to put 4 different items in one container, and make a whole slew of those containers in an array (vector). What a contreeeeeee!!!!!!!! I didn't need to overload the operator, I could have jammed the 4 things into the struct directly in main, right in that loop, then stuck the struct into the vector (array). Same ol', same ol'.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|