Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2006, 7:54 PM   #11
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
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.
Cache is offline   Reply With Quote
Old Apr 9th, 2006, 8:06 PM   #12
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
What do you mean?
codylee270 is offline   Reply With Quote
Old Apr 10th, 2006, 6:05 AM   #13
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
Using the iostream operators, you can read the data very, very easily - give it a go.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 10th, 2006, 6:27 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 10th, 2006, 4:14 PM   #15
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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!!!
codylee270 is offline   Reply With Quote
Old Apr 10th, 2006, 4:28 PM   #16
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
1 2 3 4
Those would be four separate values, for example. The values would only be formatted like this in the output file, when the program wants to deal with the values in the saved file, you would format it for human eyes, as dawei said. The output to the console after grabbing 1, 2, 3, 4 from the file, would look something like this, depending on how "pretty" you want it.
Quote:
first value - 1
second value - 2
third value - 3
fourth value - 4
Try not to make your saved file all pretty looking with astericks and '=' and other symbols, make it very simple. The input to the program is where you want it easy to grab what you're looking for. If your program is filled with all these extra useless characters, it only makes the task harder.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Apr 10th, 2006, 7:13 PM   #17
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
i totally understand now...lol...i'm a hardass (and head)!
codylee270 is offline   Reply With Quote
Old Apr 10th, 2006, 7:58 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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:
Originally Posted by Output
The input file:

Employee ID       Employee Sex         Numeric Wage        Years with Company
-----------       ------------         ------------        ------------------
       6162                  F         $      12.23                       355
    4654654                  M         $      23.60                         6

The data:

ID: 6162, Gender: F, Wage: 12.23, Longevity: 355
ID: 4654654, Gender: M, Wage: 23.6, Longevity: 6

Press ENTER to terminate program
__________________
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
DaWei is offline   Reply With Quote
Old Apr 10th, 2006, 8:53 PM   #19
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
i'm definitely not quite that clever with vectors or classes...especially classes!
codylee270 is offline   Reply With Quote
Old Apr 10th, 2006, 9:48 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC