Thread: How to redirect command prompt output to a text file

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Post How to redirect command prompt output to a text file

    One of my college assignments was to create a slot machine in c. I made it no bother, however the professor is asking that we show a few test runs of the program by writing a a sample run of the slot machine to a text file. Could someone please tell me how I would write the output of the .exe to a text file.
    kind regards!

  2. #2
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Well you can actually achieve that in several yes. The most formal one is using freopen.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        freopen ("myfile.txt","w",stdout);
    
        printf ("This will be redirected..\n");
    
        fclose (stdout);
    
        return 0;
    }
    Last edited by Al3; 02-12-2015 at 11:11 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You could also redirect your output to a file ./yourProgram > data.txt

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    Hi guys, thanks for the responses, however In my program I have user input and interaction. I added the file i/o code above to my code, I also tried the redirection> , however both make command prompt hang, and when i open the text file it is empty... Anyone have any ideas? I'm thinking I may just re-write word-for-word a sample run into notepad.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your code and show how you're running the program.


    Jim

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    So here's my code without file i/o.
    Code:
    /*
    Written by: Shane Monks O'Byrne|14337326
    One-Armed bandit slot machine
    */
    
    
    /*header files*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    /*slot struct*/
    typedef struct{
    	int one;
    	int two;
    	int three;
    }slot;
    
    
    int main(void)
    {
    	/*declaring variables*/
    	int credit = 10;
    	int bet;
    	int diff;
    	int cont;
    	int i;
    	slot play;
    	
    	/*welcome and bet input*/
    	printf("Welcome to the ONE-ARMED-BANDIT slot machine\n");
    	
    	while(credit >= 0)
    	{
    		printf("Your available credit is %d\n", credit);
    		printf("How much do you want to bet?\n");
    
    
    		while(scanf("%d", &bet) != 1)
    		{
    			while(getchar() != '\n')
    			{
    				printf("Please use numbers only\n");
    				break;
    			}
    		}
    		
    		/*if bet is invalid, this will loop*/
    		if(bet < 2 || bet > credit)
    		{
    			while(bet < 2 || bet > credit)
    			{
    				printf("\nInvalid bet(must be greater than 2 credits and <= available credit), try again:\n"); 
    				scanf("%d", &bet);
    			}
    		}
    			
    		credit -= bet;	//taking bet away from credits
    		
    		srand(time(0)); //seeding the random number generator against time
    		
    		/*Giving each value in play a random number between 1 and 3*/
    		play.one = rand()%3+1;
    		play.two = rand()%3+1;
    		play.three = rand()%3+1;
    		
    		printf("\n\n");
    		printf("|| ");
    		
    		/*Below is printing what the RNG has assigned to each slot*/
    	
    		if(play.one == 1)
    			printf("APPLE || ");
    				
    		if(play.one == 2)
    			printf("ORANGE || ");
    						
    		if(play.one == 3)
    			printf("PEAR || ");
    			
    			
    		if(play.two == 1)
    			printf("APPLE || ");
    					
    		if(play.two == 2)
    			printf("ORANGE || ");
    					
    		if(play.two == 3)
    			printf("PEAR || ");
    					
    					
    		if(play.three == 1)
    			printf("APPLE || ");
    					
    		if(play.three == 2)
    			printf("ORANGE || ");
    					
    		if(play.three == 3)
    			printf("PEAR || ");	
    	
    		printf("\n\n\n");
    		
    		/*all the different variations*/
    		
    		/*full house*/
    		if(play.one == play.two && play.two == play.three)
    		{
    			credit = credit + (2 * bet);
    			printf("** FULL HOUSE **\nYou won %d credits", bet);
    		}
    		
    		/*empty house*/
    		else if(play.one != play.two && play.two != play.three && play.one != play.three)
    		{
    			printf("You lost %d credits", bet);	
    		}
    		
    		/*half house*/
    		else
    		{
    			credit = credit + (1.5 * bet);
    			printf("** HALF HOUSE **\nYou won %d credits", bet/2);
    		}
    		
    		printf("\n\n\n");
    		printf("Current credit balance = %d\n", credit); //display remaining balance
    		printf("Play again? (y/n)\n");
    		cont = getch(); //continue?
    		
    		/*if user wants to continue*/
    		
    		if(cont != 'y' && cont != 'n')
    		{
    			while(cont != 'y' && cont != 'n')
    			{
    				printf("Please enter either 'y' or 'n':\n");
    				cont = getch();
    			}
    		}
    		
    		if(cont == 'y')
    		{
    			if(credit <= 2)//insufficient credit ends game
    			{	
    				if(credit == 1)
    					printf("Insufficient credit to continue, you only have 1 credit remaining");
    				
    				if(credit == 0)
    					printf("Insufficient credit to continue, You have 0 credits remaining");
    						
    				break;
    			}
    		}
    		
    		/*if user does not want to continue*/
    		if(cont == 'n')
    		{
    			diff = credit - 10;
    				
    			if(credit <= 10)
    				printf("End of game: total amount lost = %d credit(s)", diff);
    				
    			if(credit > 10)
    				printf("End of game: total amount won = %d credit(s)", diff);
    					
    			break;
    		}
    	}
    	
    	return 0;

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    Normally in command prompt i would: gcc slot_machine.c -o slot_machine.
    However I've tried to: slot_machine.exe > a.txt and the cmd just leaves me hangin
    I also tried fopen/fclose business and it gave the same result when run

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So, you stated in your original problem description: "Could someone please tell me how I would write the output of the .exe to a text file.". Output redirection should solve this issue. You'll still need to enter all the information into the program but the output should go a file instead of the console.

    If you want to be able to run the program without providing the input through the console, you can also redirect the input stream, but remember you'll need to get your inputs in the correct order for the program to work properly.

    Jim

  9. #9
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by jimblumberg View Post
    You could also redirect your output to a file ./yourProgram > data.txt

    Jim
    I remember such system command. If this is what you mean then I don't think invoking system command is a good idea.
    Especially in Linux. It hides a risk of S.C.I

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Who is invoking the system command? That is called output redirection. By the way input and output redirection is available on Windows as well.

    And just what is "S.C.I"?

    Jim

  11. #11
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by jimblumberg View Post
    Who is invoking the system command? That is called output redirection. By the way input and output redirection is available on Windows as well.

    And just what is "S.C.I"?

    Jim
    You can redirect with system("CMD > test.txt"); Replace CMD with the file otherwise it will print a shell-edit-mode input.
    Also Code injection - Wikipedia, the free encyclopedia

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And where in the my post do you see anything to do with the system() function?

    Jim

  13. #13
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by jimblumberg View Post
    And where in the my post do you see anything to do with the system() function?

    Jim
    Nowhere? That is why I wasn't sure and stated "if that is what you meant" because you either didn't clarify the details.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Typing out ./yourprogram followed by any arguments or redirections is the normal and completely safe way to execute a program on the command line. It is only insecure if your shell itself is insecure.

    system() need not be invoked, just redirect when you use the program itself.

  15. #15
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Quote Originally Posted by whiteflags View Post
    Typing out ./yourprogram followed by any arguments or redirections is the normal and completely safe way to execute a program on the command line. It is only insecure if your shell itself is insecure.

    system() need not be invoked, just redirect when you use the program itself.
    What would be wrong with opening a file to append and follow each printf() statement with an fprintf() statement?
    Since the code is all inline within main() it would seem an easy method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect make output to some other file..
    By jgtech in forum Linux Programming
    Replies: 1
    Last Post: 08-07-2011, 11:35 PM
  2. Command Prompt use within C# class file
    By cStudy in forum C# Programming
    Replies: 9
    Last Post: 10-06-2010, 09:14 AM
  3. command prompt input, write into file
    By angela in forum C Programming
    Replies: 3
    Last Post: 03-17-2010, 09:00 AM
  4. file output redirect from child
    By rvervecken in forum C Programming
    Replies: 4
    Last Post: 08-12-2009, 10:37 AM
  5. Output is command dos prompt not windows/frames
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 04-25-2008, 02:24 AM