Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What C++ I/O function disallows the use of stdin?

42 views
Skip to first unread message

Peng Yu

unread,
May 10, 2018, 3:51:57 PM5/10/18
to
Hi,

https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647

At the above code, it does not allow stdin.

if (args_->input == "-") {
// manage expectations
throw std::invalid_argument("Cannot use stdin for training!");
}

The relevant functions are the following. But I don't see which C++ I/O function can not be used with stdin. Does anybody see? Thanks.

https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc#L228
Dictionary::readFromFile

https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc#L201
Dictionary::readWord

Regards,
Peng

Richard

unread,
May 10, 2018, 4:12:51 PM5/10/18
to
[Please do not mail me a copy of your followup]

Peng Yu <peng...@gmail.com> spake the secret code
<a608fa67-4750-4813...@googlegroups.com> thusly:

>Hi,
>
>https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647
>
>At the above code, it does not allow stdin.
>
> if (args_->input == "-") {
> // manage expectations
> throw std::invalid_argument("Cannot use stdin for training!");
> }
>
>The relevant functions are the following. But I don't see which C++ I/O
>function can not be used with stdin. Does anybody see? Thanks.

They probably just didn't understand how to deal with the fact that
istream's are not copyable. In this situation, the easiest solution
is to use a pointer:

{
std::ifstream ifs;
std::istream *input = nullptr;
if (args_->input == "-")
{
input = &std::cin;
}
else
{
ifs.open(args_->input);
input = &ifs;
}
dict_->readFromFile(*input);
}

(If we stick the ifstream in a local scope with {}'s we don't need to
explicitly close it and worry about someone using it later in the
function.)
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Alf P. Steinbach

unread,
May 10, 2018, 4:36:19 PM5/10/18
to
I don't understand the need for pointer?

if( args_->input == "-" )
{
dict_->readFromFile( std::cin );
}
else
{
std::ifstream f{ args_->input };
if( f.fail() ) { throw "Baluba!"; }
dict_->readFromFile( f );
}

Cheers!,

- Alf

Richard

unread,
May 10, 2018, 4:52:15 PM5/10/18
to
[Please do not mail me a copy of your followup]

"Alf P. Steinbach" <alf.p.stein...@gmail.com> spake the secret code
<pd2afq$72a$1...@dont-email.me> thusly:

>I don't understand the need for pointer?
>
> if( args_->input == "-" )
> {
> dict_->readFromFile( std::cin );
> }
> else
> {
> std::ifstream f{ args_->input };
> if( f.fail() ) { throw "Baluba!"; }
> dict_->readFromFile( f );
> }

One cat, two ways to skin it :-).

Sam

unread,
May 11, 2018, 6:47:42 AM5/11/18
to
Peng Yu writes:

> Hi,
>
> https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647
>
> At the above code, it does not allow stdin.
>
> if (args_->input == "-") {
> // manage expectations
> throw std::invalid_argument("Cannot use stdin for training!");
> }
>
> The relevant functions are the following. But I don't see which C++ I/O
> function can not be used with stdin. Does anybody see? Thanks.

For starters, stdin is a C library object. But that's neither here nor
there, since std::cin is C++ library's equivalent, that's available to be
used.

There must be some particular why this program cannot make use of std::cin,
and requires an actual file to be specified. You will have to figure out
what it is by reading the rest of the code and trying to understand it.
There could be many reasons. Perhaps the code requires a writable file, and
uses std::iostream, and std::istream, which is all that std::cin is, will
not be acceptable. Perhaps the code requires a seekable std::ifstream, and
the non-seekable std::istream will not work.

Or maybe one of several other, similar, reasons. You will need to read the
code and figure it out, if you want to.

Ben Bacarisse

unread,
May 11, 2018, 8:33:52 AM5/11/18
to
Sam <s...@email-scan.com> writes:
> Peng Yu writes:
>> https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647
>>
>> At the above code, it does not allow stdin.
>>
>> if (args_->input == "-") {
>> // manage expectations
>> throw std::invalid_argument("Cannot use stdin for training!");
>> }
>>
>> The relevant functions are the following. But I don't see which C++
>> I/O function can not be used with stdin. Does anybody see? Thanks.
>
<snip>
> ... There could be many reasons. Perhaps the code requires
> a writable file, and uses std::iostream, and std::istream, which is
> all that std::cin is, will not be acceptable.

That seems unlikely since the code reads the data by calling a function
that takes a std::istream& argument.

<snip>
--
Ben.

Jorgen Grahn

unread,
May 11, 2018, 10:43:59 AM5/11/18
to
Seems likely that it's a bug, then. I had a quick look at the code,
and my impression was that the authors had focused on the linguistics
algorithms (as researchers tend to do) and not so much on the user
interface.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Paavo Helde

unread,
May 11, 2018, 11:47:14 AM5/11/18
to
On 10.05.2018 22:51, Peng Yu wrote:
> Hi,
>
> https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647
>
> At the above code, it does not allow stdin.
>
> if (args_->input == "-") {
> // manage expectations
> throw std::invalid_argument("Cannot use stdin for training!");
> }
>
> The relevant functions are the following. But I don't see which C++ I/O function can not be used with stdin. Does anybody see? Thanks.

Maybe the program is interactive, at least in the mentioned "training
mode", and expects interactive commands from std::cin.


0 new messages