473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading unformatted text from stdin

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

Jul 22 '05 #1
19 10251

"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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,


#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
Jul 22 '05 #2
Lionel B wrote:

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 don't understand.
Why did you do ignore() at the stream?
What should be the purpose of it?

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,


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
kb******@gascad.at
Jul 22 '05 #3
Lionel B wrote:
I need to read (unformatted text) from stdin up to EOF into a char
buffer;


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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #4
Mike Wahler wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

/.../


#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), 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

Jul 22 '05 #5

"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Mike Wahler wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

/.../
#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),


With the above code, not possible.
so I'd have to give getline() an eol char that
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()' ).
would never actually occur (I think '\0' would probably do it).
On many systems, it's not possible to input a '\0' character.
Shall
try it.


Did you try what I wrote?

-Mike

Jul 22 '05 #6
Mike Wahler wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Mike Wahler wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@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),


With the above code, not possible.
so I'd have to give getline() an eol char that


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()' ).


That's exactly what I was thinking of...
would never actually occur (I think '\0' would probably do it).


On many systems, it's not possible to input a '\0' character.


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.
Did you try what I wrote?


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

Jul 22 '05 #7
Karl Heinz Buchegger wrote:
Lionel B wrote:

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 don't understand.
Why did you do ignore() at the stream?
What should be the purpose of it?


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...
Well the tip is: If you want to read then it is unwise to first
throw away everything you want to read :-)
.... 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.
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'?


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

--
Lionel B.

Jul 22 '05 #8
Dietmar Kuehl wrote:
Lionel B wrote:
I need to read (unformatted text) from stdin up to EOF into
a char buffer;


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();


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

Jul 22 '05 #9
Lionel B wrote:
Dietmar Kuehl wrote:
Lionel B wrote:
I need to read (unformatted text) from stdin up to EOF into
a char buffer;


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();


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.


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

Jul 22 '05 #10

Lionel B wrote:
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.


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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #11

"Lionel B" <go****@lionelb.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Mike Wahler wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Mike Wahler wrote:
> "Lionel B" <go****@lionelb.com> wrote in message
> news:11**********************@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),


With the above code, not possible.
so I'd have to give getline() an eol char that


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()' ).


That's exactly what I was thinking of...
would never actually occur (I think '\0' would probably do it).


On many systems, it's not possible to input a '\0' character.


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.
Did you try what I wrote?


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');


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
Jul 22 '05 #12

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
Lionel B wrote:
I need to read (unformatted text) from stdin up to EOF into a char
buffer;


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.

[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

Jul 22 '05 #13
Alex Vinokur wrote:
/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.


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

--
Lionel B

Jul 22 '05 #14

"Alex Vinokur" <al****@big-foot.com> wrote in message news:34*************@individual.net...
[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

[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


Jul 23 '05 #15
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...
Jul 23 '05 #16

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:z8*******************@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?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #17
Alex Vinokur wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:z8*******************@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?


It probably does. Why?
Jul 23 '05 #18

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:nB*******************@newsread1.mlpsca01.us.t o.verio.net...
Alex Vinokur wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:z8*******************@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?


It probably does. Why?


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

Jul 23 '05 #19
Alex Vinokur wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:nB*******************@newsread1.mlpsca01.us.t o.verio.net...
Alex Vinokur wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:z8*******************@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?


It probably does. Why?

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


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.
Jul 23 '05 #20

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

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
25
by: 7stud | last post by:
I can't break out of the for loop in this example: ------ import sys lst = for line in sys.stdin: lst.append(line) break
24
by: arnuld | last post by:
I have a function named getword that read every single input from std. input. WHAT I WANTED: I want it read the word if it has less than or equal to 30 characters. Anything else beyond that...
5
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.