" After I have compiled my program I have an executable file.
To execute it from the command prompt:
test.exe <test.in >test.out
Where test.exe is the name of my compiled executable, test.in is a text file for input, test.out is a text file that your resut will be output to.

But I dont kow HOW to do it..I ve searched a lot in google and I have got this answer...
Will you Please tell me more with code..

I have a code in which there is NZEC error and so I ve thaught to redirct the input from FILE..Will you tell me please what changes should be made in my code.
In this code , input is a 2 character string , if input is db, pq ,qp or bd output will be Mirrored pair. if two spaces are entered , program will terminate else print Ordinary pair.

#include<stdio.h>
#include<string.h>
int main()
{
char n[2];
int i=0;
printf("Ready\n");
while(gets(n))
{
if(strcmp(n," ")==0) break;
if(strcmp(n,"pq")==0 || strcmp(n,"bd")==0 || strcmp(n,"qp")==0 || strcmp(n,"db")==0)
{ printf("Mirrored pair\n");i++; continue;}
else
printf("Ordinary pair\n");
i++;
}
return(0);
}

Recommended Answers

All 9 Replies

You could do it this way in Linux.

#include <stdio.h>

#define ARR_SIZE 200

int main()
{
  char ch[ARR_SIZE];
  
  fgets(ch, ARR_SIZE, stdin);/*read data1*/
  fputs(ch, stdout);/*write data2*/
  fgets(ch, ARR_SIZE, stdin);
  fputs(ch, stdout);
  fgets(ch, ARR_SIZE, stdin);
  fputs(ch, stdout);
  return 0;
}

Console line

./testfile < data1 > data2

Thanks 4 the code..but ARRAY SIZE IS NOT defined in the program question.User will enter 2 character string till 2 blank spaces are not fetched..but in this code user can input only 200 characters,after den program will terminate.


and I have not linux in my system..
In windows,How to do it..

redirection in not explained..
please help me...

and I have not linux in my system..
In windows,How to do it..

The code I posted lies within the C standard, so it should run on any machine that supports a C standard compiler. As for the redirection, that's operating system specific and I'll assume your operating system does support it..

The moral - This code should work on your system.

Thanks 4 the code..but ARRAY SIZE IS NOT defined in the program question

Your confused, this isn't a homework completion site its a site that guides programmers to answers with pointers.

Ok Sir,I ve understood a little bit , means I ve to take input from to a file and after thar I ve to read it and compare with the strings.

..its a site that guides programmers to answers with pointers.

..pun intended? :P

Your confused, this isn't a homework completion site its a site that guides programmers to answers with pointers. Your confused, this isn't a homework completion site its a site that guides programmers to answers with pointers.

Sorry Sir, but I am truly very depressed from redirection. I ve read many books, searched in google...I know what is redirection, but I ve nt understood yet how to do it ,what changes in the program shud be made. What is the relation between my compiled exe nd the function of those two fils used in redirection.

Sorry Sir, but I am truly very depressed from redirection. I ve read many books, searched in google...I know what is redirection, but I ve nt understood yet how to do it ,what changes in the program shud be made. What is the relation between my compiled exe nd the function of those two fils used in redirection.

O.K. here's a small program that will redirect(from the command line) stdin to a data file.

testit.c

#include <stdio.h>

#define ARR_SIZE 200

int main()
{
  char ch[ARR_SIZE];
  
  while (fgets(ch, ARR_SIZE, stdin))/*read data file*/
    fputs(ch, stdout);
  return 0;
}

So if call this program with a data file say data1, it should display its contents.

testit < data1

How does it work? Well the operating system will redirect stdin to the open file descriptor of data1, so any read from stdin will read the file data1..Simple right?

Try running this program on your system and tell us if it works or fails.

ok sir..i save this program as file.c
compiled it

now in cmd prmt, i wrote

file < data1

The system cant find the file specified.

The way redirection work on Windows is simple:

If you have a command line like this:

programName <fileName

Then the standard input (keyboard generally), will be replaced by a stream input from the fileName.

Obviously, if you don't have a file named "fileName", in the same directory as your program, then your file won't be found, and it will quit with an error (as your example shows).

In this program, for instance, you can run it two ways:
With redirection, you need to give it a valid file name.

Without redirection, you need to enter a line of text, and hit enter. Without redirection, you quit entering text (and end the program), by hitting Ctrl+Z, which is the end of file, in Windows and DOS.

Try running this program, and see how it works:

#include <stdio.h>

int main() {
  char ch;
  
  while((ch=getc(stdin)) != EOF) /*read data file*/
    putc(ch, stdout);
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.