how implement getline ?

R

ragi

I need write method which will be able to get line with variable maximum
length from file. I don't want use neither iostream nor stream from
standard library.
I was trying write this method but I have problem with segmentation
fault. I do sth wrong but I van't catch my mistake ....


Declaration could have been like that:

char* get_dynamic_line(FILE* cf, char** line, int& buf_len);

In definition:

- read from file buf_len characters
- if in *line can't find EOL or EOF realloc buffer (buf_len *= 2;),
realloc and read again to greater buffer and searching EOL once again ...
- return address of 'line' or NULL

And example like that:

int begin_len = 4096;
char* line = new char[begin_len];
..
..
..
if(get_dynamic_line(file, (char**)&line, begin_len))
{
do sth with line ...
}
..
..
..
delete [] line;



Can someone show me proper way or example how to write that ?
 
V

Victor Bazarov

ragi said:
I need write method which will be able to get line with variable maximum
length from file. I don't want use neither iostream nor stream from
standard library.

How about using 'std::string'? Perfect for your "growing buffer", no?
I was trying write this method but I have problem with segmentation
fault. I do sth wrong but I van't catch my mistake ....


Declaration could have been like that:

char* get_dynamic_line(FILE* cf, char** line, int& buf_len);

In definition:

- read from file buf_len characters
- if in *line can't find EOL or EOF realloc buffer (buf_len *= 2;),
realloc and read again to greater buffer and searching EOL once again ...
- return address of 'line' or NULL

And example like that:

int begin_len = 4096;
char* line = new char[begin_len];
.
.
.
if(get_dynamic_line(file, (char**)&line, begin_len))
{
do sth with line ...
}
.
.
.
delete [] line;



Can someone show me proper way or example how to write that ?

Let's do the other way around: you post what you have and we point out
your mistakes and suggest how to correct them. Deal?

V
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

ragi said:
I need write method which will be able to get line with variable maximum
length from file. I don't want use neither iostream nor stream from
standard library.
I was trying write this method but I have problem with segmentation
fault. I do sth wrong but I van't catch my mistake ....

How can we catch your mistake without seeing the code?

One of Bjarne Stroustrup's articles uses this task as an example. See page 3
of the following article:

http://www.research.att.com/~bs/new_learning.pdf

Ali

P.S. For what it's worth, here is the Turkish translation of that article:

http://acehreli.org/~ali/turkcecpp/cpp_ogrenmek.html
 
A

Alf P. Steinbach

* =?iso-8859-1?Q?Ali_=C7ehreli?=:
How can we catch your mistake without seeing the code?

One of Bjarne Stroustrup's articles uses this task as an example. See page 3
of the following article:

http://www.research.att.com/~bs/new_learning.pdf

Ali

P.S. For what it's worth, here is the Turkish translation of that article:

http://acehreli.org/~ali/turkcecpp/cpp_ogrenmek.html

Thanks for that reference.

Amazingly I find myself agreeing with most (so far it seems everything, but
there must be something I can disagree with) of what Bjarne writes! ;-)

I've saved a copy; much wisdom in there.
 

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

Similar Threads

getline problems 8
getline problem 10
getline - refresher 2
Taking a stab at getline 40
getline questions 2
getline and deleteline 1
adapting getline 77
ifstream::getline() synatx 18

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top