problem giving inout

Hey , suppose I made a simple factorial program in C , which takes a user input. I wish to give the input from a .txt file in which the number of which I want the factorial is written. How do do this simply using command prompt or anything. Please dont tell me to open the text file using fopen and f.gets functions.


code--

#include <stdio.h>

int main()
{
int n=3, fact = 1,c;

printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);

for (c = 1; c <= n; c++)
fact = fact * c;

printf("Factorial of %d = %d\n", n, fact);

return 0;
}
Please dont tell me to open the text file using fopen and f.gets functions.

Is that because you already understand how to use these, or is it because you are not permitted to use these functions?

I don't think it's clear exactly what it is that you really want.
Sir,
actually one way to give input to the program is by opening the .txt file in which my inputs are written by using fopen and such functions. I dont want to do that , what i want is that I run the program , and i write something on the console window which can link that .txt file automatically and then give me the output......
i heard somewhere that we use such a thing --
$program_name.exe<input_filename.txt
$program_name.exe<input_filename.txt
That is redirecting the standard input. In this case, the "<" symbol tells the program to take its stdin input from the file input_filename.txt instead of from the keyboard.

It doesn't require any changes to the program itself, it is simply using the operating system capabilities to alter the behaviour.
Last edited on
Thank you but sir please can you show me how to do this. I just heard such a thing from somewhere. When I run the code that i have provided and write in the console window $factorial.exe<input.txt
then nothing happens but the program just returns me the factorial of number 3 whereas in m input.txt file I have written the number 4.


Please demonstrate it how to use the four written in the input.txt file as an input to my program using $program_name.exe<input_filename.txt

Well, as far as I can tell, you are already doing the correct thing, so I don't know what is the problem. Perhaps the program is reading the wrong file?

Here is a MS page on redirection, http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
It gives an example "Redirecting command input (<)"
Sorry sir but i wish to achieve this..
http://www.daniweb.com/software-development/c/threads/185094/redirecting-input-into-my-c-program-from-a-file.-how-to-even-start

I have made that factorial program on DEV C++ and I use windows. Please tell me whether I should modify my code. Also when I click on COMPILE AND RUN button , a console window opens . Do i need to write $factorial.exe<input.txt in this window when it seeks me for an input ?

let us say my program is saved as factorial.c
and the input file is input.txt

I am making a programming assingment on an online judge
What you need to do is to open a console window of your own, where you may enter commands.

Go to the Windows "Start" button and click on Start->Run
You should see a box where you can enter the name of the program to run. Type "cmd" and click "OK".

Then you will need to either type in the full name and path of the program and input file, or navigate to the correct location where your exe file is stored, using commands such as "cd" to change directory. To change to a different drive, type the drive letter followed by a colon, like this, D:
http://command-prompt-cmd.blogspot.co.uk/2009/07/command-prompt-navigation-directories.html

Once the command prompt is showing the correct path, then you can at last type in the command factorial.exe<input.txt

This may help too: "How to use the Windows command line"
http://www.computerhope.com/issues/chusedos.htm
Last edited on
Topic archived. No new replies allowed.