Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem: fgets cannot get input from stdin

Subject: Problem: fgets cannot get input from stdin

From: Micheal

Date: 2 Dec, 2010 14:06:04

Message: 1 of 3

Hi experts,

There is a saying in the fopen api reference:
"MATLAB reserves file identifiers 0, 1, and 2 for standard input, standard
output (the screen), and standard error, respectively. "

I am using matlab 2010b.

I want my matlab program to get one line input from standard input(stdin), but fails. I try both script and MEX-file, but both fail.

================
Here is my script:
function simple_pipe()
line = fgets(0);
disp('line got from stream is %s', line);
end

When running this script, I get error "Operation is not implemented for requested file identifier."

================
Here is my MEX-file program simplepipe.cpp
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
    char line[1024]="";
    if (fgets(line, sizeof(line), stdin) !=0 )
    {
        mexPrintf("fgets got one line %s from stream\n", line);
    }
    else
    {
        mexPrintf("Error happens in fgetl\n");
    }
    return;
}

I choose VC6.0 in the "mex -setup". The compile and link is successful. However, when runnign the MEX-file, the fgets always return 0, instead of waiting for my input in matlab command line. I also try the fgets by compiling with VC6.0 and by running in Windows cmd line, the fgets can wait for my input and can work correctly.

I do not know why. Matlab does not support getting any thing from standard input in script and MEX-file?

Please point me if anything I do or understand is incorrect. I really need this feature to implement pipe communication between my matlab program and other program who spawns it.

Thanks a lot for your help. :)

Subject: Problem: fgets cannot get input from stdin

From: Jon Danisch

Date: 18 Feb, 2011 09:38:03

Message: 2 of 3

"Micheal " <zyh3738@gmail.com> wrote in message <id894c$c6v$1@fred.mathworks.com>...
> Hi experts,
>
> There is a saying in the fopen api reference:
> "MATLAB reserves file identifiers 0, 1, and 2 for standard input, standard
> output (the screen), and standard error, respectively. "
>
> I am using matlab 2010b.
>
> I want my matlab program to get one line input from standard input(stdin), but fails. I try both script and MEX-file, but both fail.
>
> ================
> Here is my script:
> function simple_pipe()
> line = fgets(0);
> disp('line got from stream is %s', line);
> end
>
> When running this script, I get error "Operation is not implemented for requested file identifier."
>
> ================
> Here is my MEX-file program simplepipe.cpp
> void mexFunction( int nlhs, mxArray *plhs[],
> int nrhs, const mxArray *prhs[])
> {
> char line[1024]="";
> if (fgets(line, sizeof(line), stdin) !=0 )
> {
> mexPrintf("fgets got one line %s from stream\n", line);
> }
> else
> {
> mexPrintf("Error happens in fgetl\n");
> }
> return;
> }
>
> I choose VC6.0 in the "mex -setup". The compile and link is successful. However, when runnign the MEX-file, the fgets always return 0, instead of waiting for my input in matlab command line. I also try the fgets by compiling with VC6.0 and by running in Windows cmd line, the fgets can wait for my input and can work correctly.
>
> I do not know why. Matlab does not support getting any thing from standard input in script and MEX-file?
>
> Please point me if anything I do or understand is incorrect. I really need this feature to implement pipe communication between my matlab program and other program who spawns it.
>
> Thanks a lot for your help. :)

I'm not sure about MEX, but you can read from stdin using .NET.

mystdin = System.Console.In;
dotNETstr=ReadToEnd(x); %also Read(), ReadBlock(), ReadLine()
disp(char(dotNetstr))

Subject: Problem: fgets cannot get input from stdin

From: David Dresden

Date: 18 Feb, 2011 13:01:04

Message: 3 of 3

A couple of pointers from the "C" world.

1. fgets returns a type of char* which is pointer to an unsized array of chars. I would use the comparison "!= NULL", which is used for pointers, and not any checks against zero.

2. A tricky point: Fixed char arrays are so-called NULL TERMINATED, so an array of dimension [LENGTH + 1] is needed to read a stream of LENGTH characters long. For example, the string "hello" actually has 6 bytes in it, although it has only 5 characters, because there is a "\0" char embedded to the end of every C-string.

So you want to do something like this:

 char your_buffer[NUMBER_OF_CHARS + 1];
 
  while(fgets(your_buffer, [NUMBER_OF_CHARS + 1, stdin) != NULL)
        puts(your_buffer);

I hope this helps.

David Dresden
http://www.LiquidNitrogenOverclocking.com
Builders of 5.0 GHz (and faster) computers
optimized for MatLab software

>
> I want my matlab program to get one line input from standard input(stdin), but fails. I try both script and MEX-file, but both fail.
>
> ================
> Here is my script:
> function simple_pipe()
> line = fgets(0);
> disp('line got from stream is %s', line);
> end
>
> When running this script, I get error "Operation is not implemented for requested file identifier."

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us