Home > Enterprise >  Redirect output to txt file on multiple lines
Redirect output to txt file on multiple lines

Time:03-31

#include<stdio.h>  

int main()   
{   
  int n, count = 1;   
  float x, average, sum = 0;   
  printf("How many numbers do you wish to test: ");  
  scanf ("%d",&n);   
  while (count <= n)   
     {   
      printf ("Enter number #%d: ",count);   
      scanf("%f", &x);   
      sum  = x;   
        count;   
     }   
    average = sum/n;   
    printf("\nThe Average is: %.2f\n", average);   
}

This program asks the user for a certain amount of numbers to be entered and then calculates the average of those numbers. I then use a input.txt file to redirect the input and direct to an output.txt file. So using the command

./main < input.txt > output.txt

My question is, how do I make it so when the output is redirected, the output appears on multiple lines instead of all in one single line. For example, when I run the command and look at my output.txt file it looks like this:

How many numbers do you wish to test: Enter number #1: Enter number #2: Enter number #3: Enter number #4: Enter number #5: The Average is: 6.00

Is there a way to make it look like this instead:

How many numbers do you wish to test: Enter number #1:
Enter number #2:
Enter number #3:
Enter number #4:
Enter number #5:
The Average is: 6.00

CodePudding user response:

The only line break you output is at the very end of your code. If you want line breaks in the output, output them wherever you want them, just as you did in the last line of code.

  •  Tags:  
  • c
  • Related