Saving the output in a text file

consider the following c program
#include<stdio.h>
#include<conio.h>

main()
{
int i,n;
clrscr();
printf("Enter limit\n");
scanf("%d",&n);
printf("I will print %d times\n",n);
for(i=0;i<n;i++)
printf("I luv C\n");
}

the output will be
Enter limit
4
I will print 4 times
I luv C
I luv C
I luv C
I luv C
This is a simple program which will print "I luv C" n times
Now i want to save the output to a text file without copy pasting the output.
Well,in that case, first compile the saved C file[{ALT} {F9}]
Now go to file menu and click DOS SHELL or DOS PROMPT in Turbo C

You are in DOS now.Lets assume the filename of the above program be some output.c.Then issue command
C:\TC\BIN\output.exe >> output.txt
In general,the command is
filename.exe >> textfile.txt
As soon as you press enter, you will be presented a black screen and you see no message.Now enter 5 and press enter.You will again see DOS prompt.Now open the text file(text file will be in same director(C:\TC\BIN)),the following will be it's content
Enter limit
I will print 5 times
I luv C
I luv C
I luv C
I luv C
I luv C

When you direct the output to a text file,2 things are important
1.Only scanning takes place in DOS.Means no output will be there.You have to remember the sequence in which you should enter values.
2.Your input is not saved in the text file as you can see ,the file contains "Enter limit" but doesn't contain what i inputted.
Note:

1.If the text file already exists,then the output will be appended(added) to the existing contents
2.This is a general procedure assuming that you have saved your c file in C:\TC\BIN\ directory.If you have saved somewhere else say C:\Pro\ then you should first navigate to that directory by using 'cd' command i.e, cd C:\Pro\ and then the remaining procedures are same
3.The text file is saved under the same directory.If you want to store it some other place then give the path
ex:
C:\TC\BIN\output.exe >> C:\output.txt
will save the output to a text file named output.txt saved in C:\
4.Do not forget to compile the file before saving it's output in a text file
5.If you use getch(); at the end ,remember to press a key.
6.In case of above program,blank screen is presented because we have used clrscr();if it's not used then you will not see a blank screen.But you see a no prompt.This is the indication that the program is running
7.Save the file before compilation.
8.The compiled file is always having the same name as the saved c file but having extension '.exe'

Tip:
if you want the contents of the textfile to be of natural output ,then after every scanf() statement issue appropriate printf() statement of the value you scanned.
Ex:if you want to make the directed output file of the above program match exatly with real output then make this change

printf("Enter limit\n");
scanf("%d",&n);
printf("%d\n",n);
now the contents of the text file will be like this
Enter limit
5
I will print 5 times
I luv C
I luv C
I luv C
I luv C
I luv C

About This Blog

There is something for everyone. !! A tech blog on various topics. Let it be devops, programming etc; we have it all covered here in layman terms :)

Total Pageviews

Back to TOP