Best way to parse a small txt file

Discussion in 'C++' started by arak123@email.com, Oct 3, 2005.

  1. Guest

    Lets say i have file with the following structure and the following
    code snippet:

    --------------------BEGIN FILE-----------------------
    name1
    name2
    name3
    name4
    ;
    name5
    ;
    name6
    --------------------END FILE-------------------------

    Note: This simplified code example was made on the fly based on
    existing code and may contain a few small mistakes so bear with me.

    Struct Person
    {
    char *name;
    Person *next;
    ~Person();
    };

    class List
    {
    private:
    Person *head;
    public:
    List(char* fileName);
    };

    void List::List(char* fileName)
    {
    ifstream fin(fileName, ios::in);
    if(fin.is_open())
    {
    fin.seekg(0, ios::end);
    int fileLength=fin.tellg();
    fin.seekg(0, ios::beg);
    char *fileContent=new char[fileLength];
    fin.read(fileContent, fileLength);

    Person *currentPerson=head=new Person();
    char* token=strtok(fileContent, "\n");
    while(token!=NULL)
    {
    if(*token != ';')
    {
    currentPerson->next=new Person();
    currentPerson=currentPerson->next;
    currentPerson->name=token;
    }
    else
    {
    delete[] token;
    }
    token=strtok(NULL, "\n");
    }
    currentPerson->next=NULL;
    }
    }

    Person::~Person()
    {
    if(name!=NULL)
    delete[] name;
    if(next!=NULL)
    delete next;
    }


    void main()
    {
    List *list=new List("file.txt");
    delete list;
    }

    What would be the fastest and safest way to handle reading the file and
    splitting it's content into multiples variable?

    -Should i keep the provided code which read the entire file in a single
    buffer, split it in multiple variables and ensure that useless tokens
    are deleted

    or

    -Should i read the file line by line in a small buffer and strcpy() the
    content i want to keep in the appropriate variables with fixed length?

    Is the first way safe memory wise or is it best practise to use the
    second one? Is there a better way?
     
    , Oct 3, 2005
    #1
    1. Advertising

  2. Guest

    Hope I'm not doing your homework for you here, but...
    Consider string over char[]. Use std::vector for storage (if
    applicable).

    So your program could be simplified to:

    #include <iterator>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>

    using namespace std;

    int main()
    {
    ifstream ff( "in.txt", ios::in );
    if ( !ff.is_open() )
    return -1;

    string name;
    vector<string> names;

    while( !ff.eof() )
    {
    ff >> name;

    if ( name != ";" && !ff.eof() )
    names.push_back( name );
    }

    // Display the list (yes, it's really only 1 line ;) )
    copy (names.begin(), names.end(), ostream_iterator<string>(cout,
    "\n"));

    return 0;
    }
     
    , Oct 3, 2005
    #2
    1. Advertising

  3. Guest

    a écrit :

    > Hope I'm not doing your homework for you here, but...
    > Consider string over char[]. Use std::vector for storage (if
    > applicable).
    >
    > So your program could be simplified to:
    >
    > #include <iterator>
    > #include <iostream>
    > #include <fstream>
    > #include <string>
    > #include <vector>
    >
    > using namespace std;
    >
    > int main()
    > {
    > ifstream ff( "in.txt", ios::in );
    > if ( !ff.is_open() )
    > return -1;
    >
    > string name;
    > vector<string> names;
    >
    > while( !ff.eof() )
    > {
    > ff >> name;
    >
    > if ( name != ";" && !ff.eof() )
    > names.push_back( name );
    > }
    >
    > // Display the list (yes, it's really only 1 line ;) )
    > copy (names.begin(), names.end(), ostream_iterator<string>(cout,
    > "\n"));
    >
    > return 0;
    > }


    Thanks for the answer but, unfortunatly, string is not an option here
    (sorry, i should have specified this earlier). Also, for the sake of
    this example, lets say i have to dynamically allocate a new variable
    for each line i want to store.

    I guess my question is more along the line of should i split the entire
    file buffer into variables myself or should i read line by line and
    copy the useful content into variables.
     
    , Oct 4, 2005
    #3
  4. red floyd Guest

    wrote:
    > a écrit :
    >
    >
    >>Hope I'm not doing your homework for you here, but...
    >>Consider string over char[]. Use std::vector for storage (if
    >>applicable).
    >>
    >>So your program could be simplified to:
    >>
    >>#include <iterator>
    >>#include <iostream>
    >>#include <fstream>
    >>#include <string>
    >>#include <vector>
    >>
    >>using namespace std;
    >>
    >>int main()
    >>{
    >> ifstream ff( "in.txt", ios::in );
    >> if ( !ff.is_open() )
    >> return -1;
    >>
    >> string name;
    >> vector<string> names;
    >>
    >> while( !ff.eof() )
    >> {
    >> ff >> name;


    common error here. Should be:
    while (ff >> name)
    {
    >>
    >> if ( name != ";" && !ff.eof() )

    if (name != ";")
    >> names.push_back( name );
    >> }
    >>
    >> // Display the list (yes, it's really only 1 line ;) )
    >> copy (names.begin(), names.end(), ostream_iterator<string>(cout,
    >>"\n"));
    >>
    >> return 0;
    >>}

    >
    >
    > Thanks for the answer but, unfortunatly, string is not an option here
    > (sorry, i should have specified this earlier). Also, for the sake of
    > this example, lets say i have to dynamically allocate a new variable
    > for each line i want to store.


    Why is string not an option? If you are taking a C++ class, why is your
    instructor deliberately going to archaic methods of string storage?
     
    red floyd, Oct 4, 2005
    #4
  5. Guest

    The existing code i have to work on uses char* everywhere and as much
    as i like string, i don't want to break the existing design by using it
    each time i modify something. This is a necessary evil.
     
    , Oct 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3RlcGhhbmU=?=

    The best way to parse an html file?

    =?Utf-8?B?U3RlcGhhbmU=?=, Oct 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    6,987
    Martin Honnen
    Oct 9, 2004
  2. Sameen
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Aug 29, 2005
  3. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,578
    Roedy Green
    Sep 15, 2011
  4. Replies:
    4
    Views:
    90
    Eric I.
    Jun 9, 2008
  5. bcdixit
    Replies:
    3
    Views:
    144
    bcdixit
    Aug 19, 2007
Loading...

Share This Page