Hi,
i have a program which is like this,
FILE *fp;
FILE *temp
fp = fopen (“d.txt”,“r”);
temp = stdin;
stdin = fp;
/some code/
stdin = temp;
In linux it is running fine. But i am getting invalid lvalue in assignment Error. How i can remove this error.
thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( void )
{
FILE *fp = NULL;
int c;
char *tty = ttyname( STDIN_FILENO );
fp = freopen( "d.txt", "r", stdin );
if ( fp == NULL )
{
perror ("freopen 1");
}
fprintf (stderr, "stdin modified\n");
while( ( c = fgetchar() ) != EOF )
{
fputchar( c );
}
fp = freopen( tty, "r", stdin );
if ( fp == NULL )
{
perror ("freopen 2");
}
fprintf (stderr, "stdin restored\n");
while( ( c = fgetchar() ) != EOF )
{
fputchar( c );
}
return (1);
}
Thanks ysinitsky. It works for me. But i want to know why the previous was not running?
Thanks once again.
Hello Debjyoti,
Consider this fragment from stdio.h
#define stdin (&_CSTD _Stdin) /* standard input file /
#define stdout (&_CSTD _Stdout) / standard output file /
#define stderr (&_CSTD _Stderr) / standard error file */
Thanks,
Yuriy