Best way to parse a small txt file

A

arak123

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?
 
I

int2str

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;
}
 
A

arak123

(e-mail address removed) 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.
 
R

red floyd

(e-mail address removed) 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)
{
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?
 
A

arak123

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top