473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how implement getline ?

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 ?
Jul 23 '05 #1
3 3050
ragi wrote:
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
Jul 23 '05 #2
"ragi" <ra*******@interia.pl> wrote in message
news:42*********************@mamut2.aster.pl...
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

Jul 23 '05 #3
* =?iso-8859-1?Q?Ali_=C7ehreli?=:
"ragi" <ra*******@interia.pl> wrote in message
news:42*********************@mamut2.aster.pl...
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


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3387
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
2
3535
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
7726
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
2132
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
5589
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
3855
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
8222
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
3439
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
33
25175
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
25179
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
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,...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.