Thread: Re: [Dev-C++] How to work with *.txt files??
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: hielke_prins <hie...@he...> - 2004-09-18 17:57:18
|
>> How do I refer to each line of my *.txt file???
=20
/*
If you don't have any clue how to read a simple textfile, the first=20
thing you
proably have to do is buying a good book on C(++). But here ya go.
=20
I usually read small plaintext files in a vector using a simple=20
wrapperclass.
Something like this (if you meant to write it in C++ at least),=20
didn't test it:
*/
=20
Class Lines
{
string *d_line;
unsigned d_nLines;
=20
public:
Lines(istream streamIn);
/*
(of course you need some simple memberfunctions
returning linecount and lines on a given index too)
*/
}
=20
Lines::Lines(istream streamIn)
:
d_nLines(0)
{
string inputLine,
*tempLine;
=20
while(getline(streamIn, inputLine))
{
d_nLines++;
tempLine =3D new string[d_nLines];
for(unsigned i =3D 0; i < d_nLines; i++)
{
tempLine[i] =3D d_line[i];
}
delete d_line;
d_line =3D tempLine; =20
}
}
=20
Regards,
Hielke
|
|
From: Per W. <pw...@ia...> - 2004-09-18 20:03:50
|
Ouch.
Don't do that.
There is the vector template class in the stl.
Do not write a method that reallocates all strings every time.
First line - allocate one string.
Second line - allocate two strings.
Third line - allocate three strings.
...
By doing this, you manage to transform a linear operation - proportional
to number of lines in the file - to become O(n^2).
If you open a 100,000 line file, you would perform 100,000^2 / 2 or
5,000,000,000 string assigns.
Never add complexity when it isn't needed.
std::vector<std::string> strings;
and then
strings.push_back(line);
or
strings.reserve(10);
strings[n++] = line;
is a lot nicer code, and tremendously much more efficient.
/Per W
On Sat, 18 Sep 2004, hielke_prins wrote:
> >> How do I refer to each line of my *.txt file???
>
> /*
> If you don't have any clue how to read a simple textfile, the first
> thing you
> proably have to do is buying a good book on C(++). But here ya go.
>
> I usually read small plaintext files in a vector using a simple
> wrapperclass.
> Something like this (if you meant to write it in C++ at least),
> didn't test it:
> */
>
> Class Lines
> {
> string *d_line;
> unsigned d_nLines;
>
> public:
> Lines(istream streamIn);
> /*
> (of course you need some simple memberfunctions
> returning linecount and lines on a given index too)
> */
> }
>
> Lines::Lines(istream streamIn)
> :
> d_nLines(0)
> {
> string inputLine,
> *tempLine;
>
> while(getline(streamIn, inputLine))
> {
> d_nLines++;
> tempLine = new string[d_nLines];
> for(unsigned i = 0; i < d_nLines; i++)
> {
> tempLine[i] = d_line[i];
> }
> delete d_line;
> d_line = tempLine;
> }
> }
>
> Regards,
> Hielke
|
|
From: ZwiQ <zw...@he...> - 2004-09-18 20:37:58
|
Per Westermark wrote:
> <>Ouch.
>
> Don't do that.
>
> There is the vector template class in the stl.
(Hielke blames himself for not reading a good C++ book himself :o)
Hm, you're right. Thanks! I said I would use a vector but in the code I
did use an simple array. Now I think of it, the code I wrote down there
without good thinking was ment to store an array of pointers to strings,
not strings themselves. Used it mainly when I had to reorganize the
order of the stored strings with manipulation members (eg. sort).
I assume that assigning pointers isn't as resourcesconsuming as
assigning strings? However, you're still right about the complexity.
Vector operations themselves all are lineair? So I should use a vector
in that case too as well?
Regards,
Hielke
Zw...@he...
> <>Do not write a method that reallocates all strings every time.
>
> First line - allocate one string.
> Second line - allocate two strings.
> Third line - allocate three strings.
> ...
>
> By doing this, you manage to transform a linear operation - proportional
> to number of lines in the file - to become O(n^2).
>
> If you open a 100,000 line file, you would perform 100,000^2 / 2 or
> 5,000,000,000 string assigns.
>
> Never add complexity when it isn't needed.
>
> std::vector<std::string> strings;
>
> and then
>
> strings.push_back(line);
>
> or
>
> strings.reserve(10);
> strings[n++] = line;
>
> is a lot nicer code, and tremendously much more efficient.
>
> /Per W
>
> On Sat, 18 Sep 2004, hielke_prins wrote:
>
>>>>How do I refer to each line of my *.txt file???
>>>>
>>>>
>>/*
>> If you don't have any clue how to read a simple textfile, the first
>>thing you
>> proably have to do is buying a good book on C(++). But here ya go.
>>
>> I usually read small plaintext files in a vector using a simple
>>wrapperclass.
>> Something like this (if you meant to write it in C++ at least),
>>didn't test it:
>>*/
>>
>>Class Lines
>>{
>> string *d_line;
>> unsigned d_nLines;
>>
>> public:
>> Lines(istream streamIn);
>> /*
>> (of course you need some simple memberfunctions
>> returning linecount and lines on a given index too)
>> */
>>}
>>
>>Lines::Lines(istream streamIn)
>>:
>> d_nLines(0)
>>{
>> string inputLine,
>> *tempLine;
>>
>> while(getline(streamIn, inputLine))
>> {
>> d_nLines++;
>> tempLine = new string[d_nLines];
>> for(unsigned i = 0; i < d_nLines; i++)
>> {
>> tempLine[i] = d_line[i];
>> }
>> delete d_line;
>> d_line = tempLine;
>> }
>>}
>>
>>Regards,
>> Hielke
>>
>>
>
>
>
>
|
|
From: Daniel K. O. <dan...@ya...> - 2004-09-18 21:50:57
|
ZwiQ wrote:
> I assume that assigning pointers isn't as resourcesconsuming as
> assigning strings?
Most STL implementations (including GCC) provide an efficient way to
copy a string. In Stroustrup's book he illustrates a basic string
implementation this way.
Try it youself:
----
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int n = 10000000; // just any big number
void strings_test()
{
cout << "Strings test, press enter to start" << endl;
cin.ignore();
string msg =
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
list<string> foo;
for (int i=0; i<n; i++)
foo.push_back(msg);
cout << "Press enter to free " << n << " strings" << endl;
cin.ignore();
}
void int_test()
{
cout << "Int test, press enter to start" << endl;
cin.ignore();
list<int> foo;
for (int i=0; i<n; i++)
foo.push_back(1);
cout << "Press enter to free " << n << " numbers" << endl;
cin.ignore();
}
int main()
{
strings_test();
int_test();
return 0;
}
----
While you execute this program, keep watching the memory your program
consumes. You'll see that copying strings doesn't duplicate the data,
they share the same representation until you change one of them. With
this program you can also compare the size alocated by both the string
list and an int list.
Daniel K. O.
|
|
From: Todd G. <pi...@ea...> - 2004-09-19 04:01:54
|
Hello,
When I run the program that shown below I receive the following compiler
output. I running dev-C++ version 4.9.8.0 on Windows 2000.
Thanks for any ideas,
Todd
Compiler: Default compiler
Building Makefile: "E:\dev\cpp\devcpp\test\Makefile.win"
Executing make...
make.exe -f "E:\dev\cpp\devcpp\test\Makefile.win" all
g++.exe -c main.cpp -o
ain.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/
Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" main.cpp: In
function `void strings_test()':
main.cpp:15: `list' undeclared (first use this function) main.cpp:15: (Each
undeclared identifier is reported only once for each
function it appears in.) main.cpp:15: parse error before `>' token
main.cpp:17: `foo' undeclared (first use this function) main.cpp: In
function `void int_test()':
main.cpp:26: parse error before `>' token make.exe: *** [main.o] Error 1
Execution terminated
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Daniel K.
> O.
> Sent: Saturday, September 18, 2004 2:51 PM
> To: zw...@he...
> Cc: dev...@li...
> Subject: Re: [Dev-C++] How to work with *.txt files??
>
>
> ZwiQ wrote:
>
> > I assume that assigning pointers isn't as resourcesconsuming as
> > assigning strings?
>
>
> Most STL implementations (including GCC) provide an efficient way to
> copy a string. In Stroustrup's book he illustrates a basic string
> implementation this way.
> Try it youself:
>
> ----
> #include <string>
> #include <vector>
> #include <iostream>
>
> using namespace std;
>
> const int n = 10000000; // just any big number
>
> void strings_test()
> {
> cout << "Strings test, press enter to start" << endl;
> cin.ignore();
> string msg =
> "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
> nopqrstuvwxyz";
> list<string> foo;
> for (int i=0; i<n; i++)
> foo.push_back(msg);
> cout << "Press enter to free " << n << " strings" << endl;
> cin.ignore();
> }
>
> void int_test()
> {
> cout << "Int test, press enter to start" << endl;
> cin.ignore();
> list<int> foo;
> for (int i=0; i<n; i++)
> foo.push_back(1);
> cout << "Press enter to free " << n << " numbers" << endl;
> cin.ignore();
> }
>
> int main()
> {
> strings_test();
> int_test();
> return 0;
> }
> ----
>
>
> While you execute this program, keep watching the memory your program
> consumes. You'll see that copying strings doesn't duplicate the data,
> they share the same representation until you change one of them. With
> this program you can also compare the size alocated by both the string
> list and an int list.
>
> Daniel K. O.
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
> Project Admins to receive an Apple iPod Mini FREE for your judgement on
> who ports your project to Linux PPC the best. Sponsored by IBM.
> Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Daniel K.
> O.
> Sent: Saturday, September 18, 2004 2:51 PM
> To: zw...@he...
> Cc: dev...@li...
> Subject: Re: [Dev-C++] How to work with *.txt files??
>
>
> ZwiQ wrote:
>
> > I assume that assigning pointers isn't as resourcesconsuming as
> > assigning strings?
>
>
> Most STL implementations (including GCC) provide an efficient way to
> copy a string. In Stroustrup's book he illustrates a basic string
> implementation this way.
> Try it youself:
>
> ----
> #include <string>
> #include <vector>
> #include <iostream>
>
> using namespace std;
>
> const int n = 10000000; // just any big number
>
> void strings_test()
> {
> cout << "Strings test, press enter to start" << endl;
> cin.ignore();
> string msg =
> "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
> nopqrstuvwxyz";
> list<string> foo;
> for (int i=0; i<n; i++)
> foo.push_back(msg);
> cout << "Press enter to free " << n << " strings" << endl;
> cin.ignore();
> }
>
> void int_test()
> {
> cout << "Int test, press enter to start" << endl;
> cin.ignore();
> list<int> foo;
> for (int i=0; i<n; i++)
> foo.push_back(1);
> cout << "Press enter to free " << n << " numbers" << endl;
> cin.ignore();
> }
>
> int main()
> {
> strings_test();
> int_test();
> return 0;
> }
> ----
>
>
> While you execute this program, keep watching the memory your program
> consumes. You'll see that copying strings doesn't duplicate the data,
> they share the same representation until you change one of them. With
> this program you can also compare the size alocated by both the string
> list and an int list.
>
> Daniel K. O.
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
> Project Admins to receive an Apple iPod Mini FREE for your judgement on
> who ports your project to Linux PPC the best. Sponsored by IBM.
> Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Daniel K. O. <dan...@ya...> - 2004-09-19 05:01:29
|
Todd Gardner wrote: >main.cpp:15: `list' undeclared (first use this function) main.cpp:15: (Each > > Sorry, I confused the headers; replace #include <vector> by #include <list> Daniel K. O. |
|
From: Per W. <pw...@ia...> - 2004-09-19 11:31:06
|
Well, don't blame yourself. The human life is meant for learning :) Logically, there isn't a difference between a vector and an array. Vector is the mathematical term for a one-dimensional array, i.e. an array with a single index. The important thing about the stl template class vector is that it doesn't grow by one every time you add one element, which would have resulted in O(n^2) complexity. A typical implementation doubles the length of the vector, whenever it needs to grow. This makes sure that the reallocation times - on average - may be ignored, and the inserts will then run in constant amortized time, i.e. the _average_ insert will consume constant time. If the vector is 1,000,000 elements long - and full - and you try to add one more entry, it grows to a length of 2,000,000 resulting in the next one million inserts not needing any reallocation. It really doesn't matter if a string assign if efficient or not. The important thing to avoid - when possible - is algorithsms with exponential growth, since the total run time will start to escalate even for extremely fast operations. /Per W On Sat, 18 Sep 2004, ZwiQ wrote: > Per Westermark wrote: > > > <>Ouch. > > > > Don't do that. > > > > There is the vector template class in the stl. > > (Hielke blames himself for not reading a good C++ book himself :o) > > Hm, you're right. Thanks! I said I would use a vector but in the code I > did use an simple array. Now I think of it, the code I wrote down there > without good thinking was ment to store an array of pointers to strings, > not strings themselves. Used it mainly when I had to reorganize the > order of the stored strings with manipulation members (eg. sort). > I assume that assigning pointers isn't as resourcesconsuming as > assigning strings? However, you're still right about the complexity. > Vector operations themselves all are lineair? So I should use a vector > in that case too as well? > > Regards, > Hielke > Zw...@he... |