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? 4 3997
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;
} in*****@gmail.com 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. ar*****@email.com wrote: in*****@gmail.com 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?
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter A. Schott |
last post by:
I've got a file that seems to come across more like a dictionary from what I can
tell. Something like the following format:
###,1,val_1,2,val_2,3,val_3,5,val_5,10,val_10...
|
by: jacob nikom |
last post by:
Hi,
I would like to store XML files in MySQL. What is the best solution:
1. Convert it to string and store it as CLOB/text
2. Serialize it and store as byte array
3. Flatten it out and create...
|
by: Vittal |
last post by:
Hello All,
I am trying to compile my application on Red Hat Linux 8 against gcc
3.2.2.
Very first file in application is failing to compile. I tried
compiling my application on Linux 7.2...
|
by: William Stacey [MVP] |
last post by:
Anyone know of some library that will parse files like following:
options {
directory "/etc";
allow-query { any; }; // This is the default
recursion no;
listen-on { 192.168.0.225;...
|
by: Alan Silver |
last post by:
Hello,
MSDN (amongst other places) is full of helpful advice on ways to do data
access, but they all seem geared to wards enterprise applications. Maybe
I'm in a minority, but I don't have those...
|
by: gs |
last post by:
let say I have to deal with various date format and I am give format string
from one of the following
dd/mm/yyyy
mm/dd/yyyy
dd/mmm/yyyy
mmm/dd/yyyy
dd/mm/yy
mm/dd/yy
dd/mmm/yy
mmm/dd/yy
|
by: raylopez99 |
last post by:
What's the best way of implementing a multi-node tree in C++? What I'm
trying to do is traverse a tree of possible chess moves given an intial
position (at the root of the tree). Since every...
|
by: David T. Ashley |
last post by:
Hi,
In my PHP code, I typically break my functions into groups (usually based on
the data type they operate on), and then include the files, i.e.:
require_once("string_functions.inc");...
|
by: |
last post by:
I've built an application that scrapes JPG images of webpages and PDF
instances of those webpages automatically from an RSS feed. References to
the scraped resources are persisted to our database....
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |