Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,123 people online right now. Registration is fast and FREE... Join Now!




sort text file with columns

 
Reply to this topicStart new topic

sort text file with columns

lacries
29 Oct, 2006 - 06:51 PM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 6


My Contributions
I am working on a project where I have to use a textfile, then sort by columns. I am trying to do an initial test of the infile to the data file and my code is not working. Any guidance with my delimma would be most appreciated.

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

int main ()
{
    ifstream inputFile;
    string date;
    string name;
    double size;

    inputFile.open("data.txt");  //opens the file
    if (!inputFile)              //test for errors
        cout << "Error opening file" << endl;
    else
    {
        getline (inputFile, date);                     //get a line of data
        inputFile >> size >> name;

        while (!inputFile.eof())
        {
            
            cout << date << "\t" << size << "\t" << name << endl;     //Display next line of data found
            inputFile >> date>> size>> name; // read the next value
        }
        cout << endl;
        inputFile.close();      //close the file
    }
    return 0;
}


the text is something like this...
CODE
08/11/2005        2603 GatorPdpLoudInstaller.log
10/06/2006         506 GEARInstall.log
08/04/2004       17336 Gone Fishing.bmp
08/04/2004       26582 Greenstone.bmp

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Sort Text File With Columns
29 Oct, 2006 - 11:19 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(lacries @ 29 Oct, 2006 - 07:51 PM) *

I am working on a project where I have to use a textfile, then sort by columns. I am trying to do an initial test of the infile to the data file and my code is not working. Any guidance with my delimma would be most appreciated.

the text is something like this...
CODE
08/11/2005        2603 GatorPdpLoudInstaller.log
10/06/2006         506 GEARInstall.log
08/04/2004       17336 Gone Fishing.bmp
08/04/2004       26582 Greenstone.bmp



Well,

You code compiles OK.. can you articulate the problem you are having?

Right now all you code does is read the file line by line.
User is offlineProfile CardPM
+Quote Post

horace
RE: Sort Text File With Columns
29 Oct, 2006 - 11:23 PM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
you were reading the first line of data incorrectly
CODE

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

int main ()
{
    ifstream inputFile;
    string date;
    string name;
    double size;

    inputFile.open("data.txt");  //opens the file
    if (!inputFile)              //test for errors
        cout << "Error opening file" << endl;
    else
    {
        //**remove getline (inputFile, date);                     //get a line of data
        inputFile >>date>>  size >> name;                        // ** include date

        while (!inputFile.eof())
        {        
            cout << date << "\t" << size << "\t" << name << endl;     //Display next line of data found
            inputFile >> date>> size>> name; // read the next value
        }
        cout << endl;
        inputFile.close();      //close the file
    }
    return 0;
}


also in your data there was a space in "GoneFishing.bmp" which would cause the program to fail

if a read fails, such as
inputFile >> date>> size>> name;
it is aborted and the faulty character is left in the input stream, if you are in loop and just try to read again it will fail again and you program goes into an endless loop. You should use the functions ios::fail() or ios::good() to validate the input, e.g.
CODE

            inputFile >> date>> size>> name; // read the next value
        if(inputFile.fail())
             { cout << "input fail"; exit(1); }


This post has been edited by horace: 29 Oct, 2006 - 11:36 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Sort Text File With Columns
29 Oct, 2006 - 11:32 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
Horace,

good catch... After you pointed it out (first day back at work with daylight savings... that's my excuse)

The fix is to extract the data thus:


CODE
inputFile >>date>>  size;
inputFile.getline( name,80); // or some maximul length, maybe 255 for long file names

User is offlineProfile CardPM
+Quote Post

lacries
RE: Sort Text File With Columns
30 Oct, 2006 - 10:37 AM
Post #5

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 6


My Contributions
QUOTE(horace @ 30 Oct, 2006 - 12:23 AM) *

you were reading the first line of data incorrectly

also in your data there was a space in "GoneFishing.bmp" which would cause the program to fail

if a read fails, such as
inputFile >> date>> size>> name;
it is aborted and the faulty character is left in the input stream, if you are in loop and just try to read again it will fail again and you program goes into an endless loop. You should use the functions ios::fail() or ios::good() to validate the input, e.g.


That was my problem exactly....did not realize this was why it was going into an infinite loop. Makes sense!

Thanks for your help....and yours too GregoryH. I am sure i will be back with other problems....big project I am working on and it is driving me nuts.
User is offlineProfile CardPM
+Quote Post

lacries
RE: Sort Text File With Columns
31 Oct, 2006 - 05:16 PM
Post #6

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 6


My Contributions
Grrrr...this is not working and I do not understand...

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

int main ()
{
    ifstream inputFile;
    string date;
    string name;
    double size;

    inputFile.open("data.txt");  //opens the file
    if (!inputFile)              //test for errors
        cout << "Error opening file" << endl;
    else
    {
        inputFile >> date >> size >> name;                //get a line of data
        while (!inputFile.eof())
        {
            
            cout << date << "\t" << size << "\t" << name << endl;     //Display next line of data found
            inputFile >> date>> size>> name; // read the next value

            if(inputFile.fail())
             { cout << "input fail"; exit(1); }
        }
        cout << endl;
        inputFile.close();      //close the file
    }
    return 0;
}


text file is...

CODE
08/11/2005        2603 GatorPdpLoudInstaller.log
10/06/2006         506 GEARInstall.log
08/04/2004       17336 Gone_Fishing.bmp
08/04/2004       26582 Greenstone.bmp

User is offlineProfile CardPM
+Quote Post

lacries
RE: Sort Text File With Columns
31 Oct, 2006 - 09:21 PM
Post #7

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 6


My Contributions
QUOTE(lacries @ 31 Oct, 2006 - 06:16 PM) *

Grrrr...this is not working and I do not understand...

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

int main ()
{
    ifstream inputFile;
    string date;
    string name;
    double size;

    inputFile.open("data.txt");  //opens the file
    if (!inputFile)              //test for errors
        cout << "Error opening file" << endl;
    else
    {
        inputFile >> date >> size >> name;                //get a line of data
        while (!inputFile.eof())
        {
            
            cout << date << "\t" << size << "\t" << name << endl;     //Display next line of data found
            inputFile >> date>> size>> name; // read the next value

            if(inputFile.fail())
             { cout << "input fail"; exit(1); }
        }
        cout << endl;
        inputFile.close();      //close the file
    }
    return 0;
}


text file is...

CODE
08/11/2005        2603 GatorPdpLoudInstaller.log
10/06/2006         506 GEARInstall.log
08/04/2004       17336 Gone_Fishing.bmp
08/04/2004       26582 Greenstone.bmp



I am so good...sometimes I amaze myself....the txt file I was actually using had a white space in it that was really causing the problem....make it go away and viola....it actually worked!!! So ....jusst in case anyone was thinking I was off my rocker or somethinng....lol
User is offlineProfile CardPM
+Quote Post

horace
RE: Sort Text File With Columns
31 Oct, 2006 - 11:40 PM
Post #8

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
you could have a problem that end of file is being treated as an error so it would be worth making sure you don't call exit(1) on eof, e.g.
CODE

        while (!inputFile.eof())
        {      
            cout << date << "\t" << size << "\t" << name << endl;     //Display next line of data found
            inputFile >> date>> size>> name; // read the next value            
            if((!inputFile.eof()) && inputFile.fail())
                 { cout << "input fail"; exit(1); }
        }
        cout << " all data read!" << endl;



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month