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