Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading unformatted text from stdin

Lionel B
Guest
 
Posts: n/a
#1: Jul 23 '05
Greetings,

I need to read (unformatted text) from stdin up to EOF into a char
buffer; of course I cannot allocate my buffer until I know how much
text is available, and I do not know how much text is available until I
have read it... which seems to imply that multiple reads of the input
stream will be inevitable.

Now I can correctly find the number of characters available by:
|
| #include <iostream>
|
| std::cin.ignore(std::numeric_limits<int>::max());
| const int num_chars = std::cin.gcount();
|
Then I would like to do:
|
| char* const text = new char[num_chars+1];
| std::cin.read(text,num_chars);
| text[num_chars]='\0';
|
but the read() won't work because, as I understand it, ignore() has (as
its name implies) `thrown away' all characters in the stream...!

I am sure this must be a stock situation, and wonder if there is an
(efficient, elegant) stock way of approaching it.
Any tips appreciated,

--
Lionel B


Mike Wahler
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Reading unformatted text from stdin



"Lionel B" <google@lionelb.com> wrote in message
news:1105548836.371623.260830@z14g2000cwz.googlegr oups.com...[color=blue]
> Greetings,
>
> I need to read (unformatted text) from stdin up to EOF into a char
> buffer; of course I cannot allocate my buffer until I know how much
> text is available, and I do not know how much text is available until I
> have read it... which seems to imply that multiple reads of the input
> stream will be inevitable.
>
> Now I can correctly find the number of characters available by:
> |
> | #include <iostream>
> |
> | std::cin.ignore(std::numeric_limits<int>::max());
> | const int num_chars = std::cin.gcount();
> |
> Then I would like to do:
> |
> | char* const text = new char[num_chars+1];
> | std::cin.read(text,num_chars);
> | text[num_chars]='\0';
> |
> but the read() won't work because, as I understand it, ignore() has (as
> its name implies) `thrown away' all characters in the stream...!
>
> I am sure this must be a stock situation, and wonder if there is an
> (efficient, elegant) stock way of approaching it.
> Any tips appreciated,[/color]

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
std::cout << "Enter text: ";
std::string line;
std::getline(std::cin, line);

if(!line.empty())
{
char * const text = new char[line.size() + 1];
std::copy(line.begin(), line.end(), text);
text[line.size()] = 0;
std::cout << text << '\n';
delete[] text;
}

return 0;
}

-Mike


Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Reading unformatted text from stdin


Lionel B wrote:[color=blue]
>
> Greetings,
>
> I need to read (unformatted text) from stdin up to EOF into a char
> buffer; of course I cannot allocate my buffer until I know how much
> text is available, and I do not know how much text is available until I
> have read it... which seems to imply that multiple reads of the input
> stream will be inevitable.
>
> Now I can correctly find the number of characters available by:
> |
> | #include <iostream>
> |
> | std::cin.ignore(std::numeric_limits<int>::max());
> | const int num_chars = std::cin.gcount();
> |
> Then I would like to do:
> |
> | char* const text = new char[num_chars+1];
> | std::cin.read(text,num_chars);
> | text[num_chars]='\0';
> |
> but the read() won't work because, as I understand it, ignore() has (as
> its name implies) `thrown away' all characters in the stream...![/color]

I don't understand.
Why did you do ignore() at the stream?
What should be the purpose of it?
[color=blue]
>
> I am sure this must be a stock situation, and wonder if there is an
> (efficient, elegant) stock way of approaching it.
> Any tips appreciated,[/color]

Well the tip is: If you want to read then it is unwise to first
throw away everything you want to read :-)

Besides:
Did you know that std::string can hold a very long text?
Did you know that there is a function getline for strings?
Did you know that you can tell getline() what it should use as
delimiter for 'lines'?

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Dietmar Kuehl
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Reading unformatted text from stdin


Lionel B wrote:[color=blue]
> I need to read (unformatted text) from stdin up to EOF into a char
> buffer;[/color]

What's wrong with this?

| std::ifstream in("some file", std::ios_base::binary);
| std::ostringstream tmp;
| tmp << in.rdbuf();
| std::string const& contents = tmp.str();

.... or:

| std::ifstream in("some file",
std::ios_base::binary);
| std::istreambuf_iterator<char> beg(in), end;
| std::string contents(beg, end);

Actually, I like the latter better but it is probably considerably
slower than the first alternative on most implementations.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Lionel B
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Reading unformatted text from stdin


Mike Wahler wrote:[color=blue]
> "Lionel B" <google@lionelb.com> wrote in message
> news:1105548836.371623.260830@z14g2000cwz.googlegr oups.com...[color=green]
> > Greetings,
> >
> > I need to read (unformatted text) from stdin up to EOF into a
> > char buffer; of course I cannot allocate my buffer until I know
> > how much text is available, and I do not know how much text is
> > available until I have read it... which seems to imply that
> > multiple reads of the input stream will be inevitable.
> >
> > /.../[/color]
>
> #include <algorithm>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter text: ";
> std::string line;
> std::getline(std::cin, line);[/color]

I guess that might work, but my input might contain '\n' chars (i.e.
the default eol chars), so I'd have to give getline() an eol char that
would never actually occur (I think '\0' would probably do it). Shall
try it.

Cheers,

--
Lionel B

Mike Wahler
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Reading unformatted text from stdin



"Lionel B" <google@lionelb.com> wrote in message
news:1105551208.413318.310450@c13g2000cwb.googlegr oups.com...[color=blue]
> Mike Wahler wrote:[color=green]
> > "Lionel B" <google@lionelb.com> wrote in message
> > news:1105548836.371623.260830@z14g2000cwz.googlegr oups.com...[color=darkred]
> > > Greetings,
> > >
> > > I need to read (unformatted text) from stdin up to EOF into a
> > > char buffer; of course I cannot allocate my buffer until I know
> > > how much text is available, and I do not know how much text is
> > > available until I have read it... which seems to imply that
> > > multiple reads of the input stream will be inevitable.
> > >
> > > /.../[/color]
> >
> > #include <algorithm>
> > #include <iostream>
> > #include <string>
> >
> > int main()
> > {
> > std::cout << "Enter text: ";
> > std::string line;
> > std::getline(std::cin, line);[/color]
>
> I guess that might work, but my input might contain '\n' chars (i.e.
> the default eol chars),[/color]

With the above code, not possible.
[color=blue]
> so I'd have to give getline() an eol char that[/color]

The default 'termination' character for 'std::getline()' is '\n'
(this can be overridden by supplying a different one as the
third argument to 'std::getline()' ).
[color=blue]
> would never actually occur (I think '\0' would probably do it).[/color]

On many systems, it's not possible to input a '\0' character.
[color=blue]
>Shall
> try it.[/color]

Did you try what I wrote?

-Mike



Lionel B
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Reading unformatted text from stdin


Mike Wahler wrote:[color=blue]
> "Lionel B" <google@lionelb.com> wrote in message
> news:1105551208.413318.310450@c13g2000cwb.googlegr oups.com...[color=green]
> > Mike Wahler wrote:[color=darkred]
> > > "Lionel B" <google@lionelb.com> wrote in message
> > > news:1105548836.371623.260830@z14g2000cwz.googlegr oups.com...
> > > > Greetings,
> > > >
> > > > /.../
> > >
> > > #include <algorithm>
> > > #include <iostream>
> > > #include <string>
> > >
> > > int main()
> > > {
> > > std::cout << "Enter text: ";
> > > std::string line;
> > > std::getline(std::cin, line);[/color]
> >
> > I guess that might work, but my input might contain
> > '\n' chars (i.e. the default eol chars),[/color]
>
> With the above code, not possible.
>[color=green]
> > so I'd have to give getline() an eol char that[/color]
>
> The default 'termination' character for 'std::getline()' is '\n'
> (this can be overridden by supplying a different one as the
> third argument to 'std::getline()' ).[/color]

That's exactly what I was thinking of...
[color=blue][color=green]
> > would never actually occur (I think '\0' would probably do it).[/color]
>
> On many systems, it's not possible to input a '\0' character.[/color]

In my case input may be redirected from a file or piped from another
program, so input is not predictable (that's what I meant by
"unformatted" in my original post). However, a '\0' could be considered
pathological.
[color=blue]
> Did you try what I wrote?[/color]

Yes. As expected, it fails if the input comprises multiple lines (only
the first line is read). However, it works ok if I replace:

std::getline(std::cin, line);

with:

std::getline(std::cin, line, '\0');


Cheers,

--
Lionel B

Lionel B
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Reading unformatted text from stdin


Karl Heinz Buchegger wrote:[color=blue]
> Lionel B wrote:[color=green]
> >
> > Greetings,
> >
> > I need to read (unformatted text) from stdin up to EOF into a char
> > buffer; of course I cannot allocate my buffer until I know how much
> > text is available, and I do not know how much text is available
> > until I have read it... which seems to imply that multiple reads of
> > the input stream will be inevitable.
> >
> > Now I can correctly find the number of characters available by:
> > |
> > | #include <iostream>
> > |
> > | std::cin.ignore(std::numeric_limits<int>::max());
> > | const int num_chars = std::cin.gcount();
> > |
> > Then I would like to do:
> > |
> > | char* const text = new char[num_chars+1];
> > | std::cin.read(text,num_chars);
> > | text[num_chars]='\0';
> > |
> > but the read() won't work because, as I understand it, ignore()
> > has (as its name implies) `thrown away' all characters in the
> > stream...![/color]
>
> I don't understand.
> Why did you do ignore() at the stream?
> What should be the purpose of it?[/color]

It was just a first cut attempt at calculating the number of characters
in the stream. The "advantage" is that it will, if invoked as above,
read to the end of the stream and enable gcount() to return the number
of characters correctly. Of course it has a fatal disadvantage...
[color=blue]
> Well the tip is: If you want to read then it is unwise to first
> throw away everything you want to read :-)[/color]

.... of course ;-) What I really need is an ignore() that doesn't
ignore; i.e. an unformatted read call which reads to the end of the
stream but doesn't actually extract any chars. As far as I know no
such call exists; I suspect it may be possible to do something along
these lines with peek() in a loop. Might give that a try.
[color=blue]
> Besides:
> Did you know that std::string can hold a very long text?
> Did you know that there is a function getline for strings?
> Did you know that you can tell getline() what it should use as
> delimiter for 'lines'?[/color]

Yep. See Mike Wahler's suggestion and my reply.
Regards,

--
Lionel B.

Lionel B
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Reading unformatted text from stdin


Dietmar Kuehl wrote:[color=blue]
> Lionel B wrote:[color=green]
> > I need to read (unformatted text) from stdin up to EOF into
> > a char buffer;[/color]
>
> What's wrong with this?
>
> | std::ifstream in("some file", std::ios_base::binary);
> | std::ostringstream tmp;
> | tmp << in.rdbuf();
> | std::string const& contents = tmp.str();[/color]

Works well. I have also tried:

| const int nchars= std::in.rdbuf()->in_avail()-1;
| char* const text = new char[nchars+1];
| std::in.read(text,nchars);
| text[nchars]='\0';

(needs the "-1" in the 1st line; I guess in_avail() counts the EOF too)
which should be pretty efficient, although I am not quite sure whether
in_avail() will always give me what I expect (i.e. the entire input up
to EOF). Seems to work, though.

Cheers,

--
Lionel B

Lionel B
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Reading unformatted text from stdin


Lionel B wrote:[color=blue]
> Dietmar Kuehl wrote:[color=green]
> > Lionel B wrote:[color=darkred]
> > > I need to read (unformatted text) from stdin up to EOF into
> > > a char buffer;[/color]
> >
> > What's wrong with this?
> >
> > | std::ifstream in("some file", std::ios_base::binary);
> > | std::ostringstream tmp;
> > | tmp << in.rdbuf();
> > | std::string const& contents = tmp.str();[/color]
>
> Works well. I have also tried:
>
> | const int nchars= std::in.rdbuf()->in_avail()-1;
> | char* const text = new char[nchars+1];
> | std::in.read(text,nchars);
> | text[nchars]='\0';
>
> (needs the "-1" in the 1st line; I guess in_avail() counts the
> EOF too) which should be pretty efficient, although I am not quite
> sure whether in_avail() will always give me what I expect (i.e. the
> entire input up to EOF). Seems to work, though.[/color]

Correction: seems to work *sometimes* :-/ If I pipe input in from
another program (my prog reads stdin; i.e. in = cin) then sometimes
in_avail() returns 0... maybe some synching/flushing issue? Or cruddy
implementation of pipes (this is Win2k)? Tricky to replicate exact
conditions under which it doesn't work.
So I'm sticking with Dietmar's first method for now.

--
Lionel B

Dietmar Kuehl
Guest
 
Posts: n/a
#11: Jul 23 '05

re: Reading unformatted text from stdin



Lionel B wrote:[color=blue]
> Works well. I have also tried:
>
> | const int nchars= std::in.rdbuf()->in_avail()-1;
> | char* const text = new char[nchars+1];
> | std::in.read(text,nchars);
> | text[nchars]='\0';
>
> (needs the "-1" in the 1st line; I guess in_avail() counts the EOF[/color]
too)[color=blue]
> which should be pretty efficient, although I am not quite sure[/color]
whether[color=blue]
> in_avail() will always give me what I expect (i.e. the entire input[/color]
up[color=blue]
> to EOF). Seems to work, though.[/color]

This does not work: 'in_avail()' returns the number of characters
in the stream buffer's buffer. However, this number is not at all
related to the number of characters to be expected from the stream.
The use of 'in_avail()' is actually very limited - personally I had
no good use for 'in_avail()', yet.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Mike Wahler
Guest
 
Posts: n/a
#12: Jul 23 '05

re: Reading unformatted text from stdin



"Lionel B" <google@lionelb.com> wrote in message
news:1105608206.182627.77750@c13g2000cwb.googlegro ups.com...[color=blue]
> Mike Wahler wrote:[color=green]
> > "Lionel B" <google@lionelb.com> wrote in message
> > news:1105551208.413318.310450@c13g2000cwb.googlegr oups.com...[color=darkred]
> > > Mike Wahler wrote:
> > > > "Lionel B" <google@lionelb.com> wrote in message
> > > > news:1105548836.371623.260830@z14g2000cwz.googlegr oups.com...
> > > > > Greetings,
> > > > >
> > > > > /.../
> > > >
> > > > #include <algorithm>
> > > > #include <iostream>
> > > > #include <string>
> > > >
> > > > int main()
> > > > {
> > > > std::cout << "Enter text: ";
> > > > std::string line;
> > > > std::getline(std::cin, line);
> > >
> > > I guess that might work, but my input might contain
> > > '\n' chars (i.e. the default eol chars),[/color]
> >
> > With the above code, not possible.
> >[color=darkred]
> > > so I'd have to give getline() an eol char that[/color]
> >
> > The default 'termination' character for 'std::getline()' is '\n'
> > (this can be overridden by supplying a different one as the
> > third argument to 'std::getline()' ).[/color]
>
> That's exactly what I was thinking of...
>[color=green][color=darkred]
> > > would never actually occur (I think '\0' would probably do it).[/color]
> >
> > On many systems, it's not possible to input a '\0' character.[/color]
>
> In my case input may be redirected from a file or piped from another
> program, so input is not predictable (that's what I meant by
> "unformatted" in my original post). However, a '\0' could be considered
> pathological.
>[color=green]
> > Did you try what I wrote?[/color]
>
> Yes. As expected, it fails if the input comprises multiple lines (only
> the first line is read). However, it works ok if I replace:
>
> std::getline(std::cin, line);
>
> with:
>
> std::getline(std::cin, line, '\0');[/color]

Alternatively you can read line-by-line in a loop:

while(std::getline(std::cin, line))
{
/* do stuff */
}
if(!std.cin.eof())
/* error occurred while reading */

This form will allow you to do error checking for
each line, if that helps at all (e.g. it could let
you spot a possible stray '\0' 'mid-stream')

-Mike


Alex Vinokur
Guest
 
Posts: n/a
#13: Jul 23 '05

re: Reading unformatted text from stdin



"Dietmar Kuehl" <dietmar_kuehl@yahoo.com> wrote in message news:1105551054.428029.184500@f14g2000cwb.googlegr oups.com...[color=blue]
> Lionel B wrote:[color=green]
> > I need to read (unformatted text) from stdin up to EOF into a char
> > buffer;[/color]
>
> What's wrong with this?
>
> | std::ifstream in("some file", std::ios_base::binary);
> | std::ostringstream tmp;
> | tmp << in.rdbuf();
> | std::string const& contents = tmp.str();
>
> ... or:
>
> | std::ifstream in("some file",
> std::ios_base::binary);
> | std::istreambuf_iterator<char> beg(in), end;
> | std::string contents(beg, end);
>
> Actually, I like the latter better but it is probably considerably
> slower than the first alternative on most implementations.[/color]
[snip]


Testing "Reading contents from file into one string" with using C/C++ Perfometer.

Summary
http://groups-beta.google.com/group/...01f89772746dc3
http://groups-beta.google.com/group/...str.txt?part=2

================================================== ======
| | File size |
| Testsuite |------------------------|
| | 1000 : 10000 : 100000 |
|------------------------------------------------------|
| getline | 125 : 839 : 7729 |
| vector, reading char | 75 : 592 : 5816 |
| string, reading char | 71 : 570 : 5626 |
| vector, reading whole file | 16 : 30 : 146 |
| mmap (UNIX) | 13 : 18 : 30 |
| istream_iterator | 80 : 597 : 6028 |
| ostringstream, rdbuf | 15 : 20 : 66 |
| istreambuf_iterator | 30 : 71 : 624 |
================================================== ======

We can see that speed differences between the testsuites are sizeable.

Full raw run log:
http://groups-beta.google.com/group/...7160243fd793cb
http://groups-beta.google.com/group/...str.txt?part=2



--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Lionel B
Guest
 
Posts: n/a
#14: Jul 23 '05

re: Reading unformatted text from stdin


Alex Vinokur wrote:[color=blue]
> /snip/
>
> Testing "Reading contents from file into one string" with using
> C/C++ Perfometer.
>
> Summary
> http://groups-beta.google.com/group/...01f89772746dc3
>[/color]
http://groups-beta.google.com/group/...str.txt?part=2[color=blue]
>
> ================================================== ======
> | | File size |
> | Testsuite |------------------------|
> | | 1000 : 10000 : 100000 |
> |------------------------------------------------------|
> | getline | 125 : 839 : 7729 |
> | vector, reading char | 75 : 592 : 5816 |
> | string, reading char | 71 : 570 : 5626 |
> | vector, reading whole file | 16 : 30 : 146 |
> | mmap (UNIX) | 13 : 18 : 30 |
> | istream_iterator | 80 : 597 : 6028 |
> | ostringstream, rdbuf | 15 : 20 : 66 |
> | istreambuf_iterator | 30 : 71 : 624 |
> ================================================== ======
>
> We can see that speed differences between the testsuites
> are sizeable.[/color]

Thanks, that's interesting... I'll probably stick with the
ostringstream/rdbuf version.

--
Lionel B

Alex Vinokur
Guest
 
Posts: n/a
#15: Jul 23 '05

re: Reading unformatted text from stdin



"Alex Vinokur" <alexvn@big-foot.com> wrote in message news:34nqrjF4ep0btU1@individual.net...
[snip][color=blue]
>
> Testing "Reading contents from file into one string" with using C/C++ Perfometer.
>
> Summary
> http://groups-beta.google.com/group/...01f89772746dc3
> http://groups-beta.google.com/group/...str.txt?part=2
>
> ================================================== ======
> | | File size |
> | Testsuite |------------------------|
> | | 1000 : 10000 : 100000 |
> |------------------------------------------------------|
> | getline | 125 : 839 : 7729 |
> | vector, reading char | 75 : 592 : 5816 |
> | string, reading char | 71 : 570 : 5626 |
> | vector, reading whole file | 16 : 30 : 146 |
> | mmap (UNIX) | 13 : 18 : 30 |
> | istream_iterator | 80 : 597 : 6028 |
> | ostringstream, rdbuf | 15 : 20 : 66 |
> | istreambuf_iterator | 30 : 71 : 624 |
> ================================================== ======
>
> We can see that speed differences between the testsuites are sizeable.
>
> Full raw run log:
> http://groups-beta.google.com/group/...7160243fd793cb
> http://groups-beta.google.com/group/...str.txt?part=2
>[/color]
[snip]


Extended set of testsuites for "Reading contents from file into one string"
contains 29 testsuites:
* 4 - for C language,
* 1 - for UNIX system calls,
* 24 - for C++ language.


Testing "Reading contents from file into one string"
with using Simple C/C++ Perfometer.
---------------------------
* http://groups-beta.google.com/group/...73f4d1a05cfbd1
* http://article.gmane.org/gmane.comp....perfometer/110
* http://permalink.gmane.org/gmane.com...perfometer/110
* http://comments.gmane.org/gmane.comp...perfometer/110
* http://cache.gmane.org/gmane/comp/la...perfometer/110
---------------------------


Environment: Windows 2000, Cygwin
File size = 10000

Test results sorted by ascending time used
(the best of time used in binary and text openmode).

================================================== =========================
| Testsuite | File OpenMode |
|-------------------------------------------------------|-----------------|
| Code : Name | binary : text |
|-------------------------------------------------------|-----------------|
| C-04 : C-function fread, max size buffer | 10 : 10 |
| C-03 : C-function fread, const size buffer | 10 : 13 |
| CPP-24 : std::string and istream::read | 10 : 16 |
| Unix-C-05 : UNIX system call mmap | 13 : 53 |
| CPP-05 : istream::read, ostream::write, | 20 : 30 |
| : const size buffer | : |
| CPP-06 : istream::read, ostream::write, | 20 : 30 |
| : ostringstream, const size buffer | : |
| CPP-04 : ifstream::rdbuf, ostream::operator<< | 20 : 33 |
| CPP-08 : istream::read, ostream::write | 23 : 30 |
| : max size buffer | : |
| CPP-03 : streambuf::sbumpc, ostream::operator<< | 26 : 30 |
| CPP-23 : std::vector, istream::read | 33 : 43 |
| CPP-07 : istream::readsome, ostream::write | 53 : 60 |
| : const size buffer | : |
| CPP-11 : istream::getline, ostringstream | 56 : 57 |
| : ostream::operator<< | 56 : 57 |
| CPP-14 : istream::get(char*, streamsize), | 56 : 57 |
| : ostream::operator<<, const size | : |
| CPP-15 : istream::get(streambuf&), streambuf, | 163 : 76 |
| : ostream::operator<< | : |
| CPP-20 : istreambuf_iterator, std::string | 193 : 167 |
| CPP-18 : istreambuf_iterator, ostreambuf_iterator, | 187 : 370 |
| : std::copy | : |
| CPP-19 : istreambuf_iterator, ostreambuf_iterator, | 270 : 190 |
| : std::transform | : |
| CPP-13 : istream::get(char) | 1442 : 1428 |
| CPP-22 : std::vector, push_back() | 1502 : 1452 |
| CPP-17 : istream_iterator, std::string | 1542 : 1592 |
| CPP-09 : std::getline, ostringstream, | 1656 : 1619 |
| : ostream::operator<< | 1656 : 1619 |
| CPP-02 : streambuf::sbumpc | 1625 : 1652 |
| CPP-10 : std::getline, std::string, | 1665 : 1652 |
| : ostream::operator<< | : |
| C-02 : C-function fgetc | 1939 : 1966 |
| C-01 : C-function getc | 2002 : 1983 |
| CPP-21 : std::vector, std::copy | 2827 : 2890 |
| CPP-12 : istream::get(char), ostream::put | 2830 : 2841 |
| CPP-01 : istream::operator>> | 2951 : 2957 |
| CPP-16 : istream_iterator, ostream_iterator, | 4232 : 4269 |
| : std::copy | : |
================================================== =========================


We can see that the best method of the C++ methods is CPP-24
that uses std::string and istream::read()

====== CPP-24 method ======
string str (infile_size, '0');
infile.read(&ret_str[0], infile_size);
return str;
===========================


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




Victor Bazarov
Guest
 
Posts: n/a
#16: Jul 23 '05

re: Reading unformatted text from stdin


Alex Vinokur wrote:[color=blue]
> [...]
> We can see that the best method of the C++ methods is CPP-24
> that uses std::string and istream::read()
>
> ====== CPP-24 method ======
> string str (infile_size, '0');
> infile.read(&ret_str[0], infile_size);
> return str;
> ===========================[/color]

.... which probably falls back onto C method for reading a buffer of
a known size using fread. You could have just asked...
Alex Vinokur
Guest
 
Posts: n/a
#17: Jul 23 '05

re: Reading unformatted text from stdin



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:z8V0e.56515$NC6.42291@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> Alex Vinokur wrote:[color=green]
> > [...]
> > We can see that the best method of the C++ methods is CPP-24
> > that uses std::string and istream::read()
> >
> > ====== CPP-24 method ======
> > string str (infile_size, '0');
> > infile.read(&ret_str[0], infile_size);
> > return str;
> > ===========================[/color]
>
> ... which probably falls back onto C method for reading a buffer of
> a known size using fread. You could have just asked...[/color]

Does ifstream have access to file-pointer of the same file?


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Victor Bazarov
Guest
 
Posts: n/a
#18: Jul 23 '05

re: Reading unformatted text from stdin


Alex Vinokur wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:z8V0e.56515$NC6.42291@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>Alex Vinokur wrote:
>>[color=darkred]
>>>[...]
>>>We can see that the best method of the C++ methods is CPP-24
>>> that uses std::string and istream::read()
>>>
>>>====== CPP-24 method ======
>>>string str (infile_size, '0');
>>> infile.read(&ret_str[0], infile_size);
>>> return str;
>>>===========================[/color]
>>
>>... which probably falls back onto C method for reading a buffer of
>>a known size using fread. You could have just asked...[/color]
>
>
> Does ifstream have access to file-pointer of the same file?[/color]

It probably does. Why?
Alex Vinokur
Guest
 
Posts: n/a
#19: Jul 23 '05

re: Reading unformatted text from stdin



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:nBV0e.56523$NC6.32078@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> Alex Vinokur wrote:[color=green]
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:z8V0e.56515$NC6.42291@newsread1.mlpsca01.us.t o.verio.net...
> >[color=darkred]
> >>Alex Vinokur wrote:
> >>
> >>>[...]
> >>>We can see that the best method of the C++ methods is CPP-24
> >>> that uses std::string and istream::read()
> >>>
> >>>====== CPP-24 method ======
> >>>string str (infile_size, '0');
> >>> infile.read(&ret_str[0], infile_size);
> >>> return str;
> >>>===========================
> >>
> >>... which probably falls back onto C method for reading a buffer of
> >>a known size using fread. You could have just asked...[/color]
> >
> >
> > Does ifstream have access to file-pointer of the same file?[/color]
>
> It probably does. Why?[/color]

But we can't do that in our C++ programs (?).

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Victor Bazarov
Guest
 
Posts: n/a
#20: Jul 23 '05

re: Reading unformatted text from stdin


Alex Vinokur wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:nBV0e.56523$NC6.32078@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>Alex Vinokur wrote:
>>[color=darkred]
>>>"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:z8V0e.56515$NC6.42291@newsread1.mlpsca01.us.t o.verio.net...
>>>
>>>
>>>>Alex Vinokur wrote:
>>>>
>>>>
>>>>>[...]
>>>>>We can see that the best method of the C++ methods is CPP-24
>>>>> that uses std::string and istream::read()
>>>>>
>>>>>====== CPP-24 method ======
>>>>>string str (infile_size, '0');
>>>>> infile.read(&ret_str[0], infile_size);
>>>>> return str;
>>>>>===========================
>>>>
>>>>... which probably falls back onto C method for reading a buffer of
>>>>a known size using fread. You could have just asked...
>>>
>>>
>>>Does ifstream have access to file-pointer of the same file?[/color]
>>
>>It probably does. Why?[/color]
>
>
> But we can't do that in our C++ programs (?).[/color]

Do what? You can have your own file-pointer, can't you? No, you cannot
obtain the inner data of an ifstream if that's what you're asking. It is
called the "data hiding" or "data abstraction" principle. You're not
supposed to care how ifstream deals with opening files and reading its
data. It does not have to be a file-pointer. It probably is, but it does
not have to be.
Closed Thread


Similar C / C++ bytes