Redirect C output to a txt file - c

I am redirecting program output to a txt file and entering the values prompted by the main function:
I expected my output.txt file to have the following data:
Enter float1: 2.4
Enter float1: 4.6
But instead, it has this:
Enter float1:
Enter float1:
Here is my main function:
printf("Enter float1: ");
scanf("%f", &float1);
printf("Enter float2: ");
scanf("%f", &float2);
Can anyone please help to redirect inputted values as well?
I researched the question but I couldn't find anything that would solve the problem.
I tried redirection stderr to see if the values are printed there but it gave me an empty file.

The user input is stdin but you are redirecting only stdout to file. Hence you will see in the output.txt only what your program prints to stdout. If you want to print entered values, you have to print them after scanf.
In the example below, you should see also input values in your output.txt
printf("Enter float1: ");
scanf("%f", &float1);
printf("%f\n", float1);

You got the two values with scanf() but you didn't print them (with printf() for example), so they don't appear in stdout.
When we input something on the standard input with the keyboard, we can see the text just because the terminal (not your program) echoes these characters.
So, redirecting your program's standard output does not change anything to that.

Change your main to:
printf("Enter float1: ");
scanf("%f", &float1);
printf("%f\n", float1);
printf("Enter float2: ");
scanf("%f", &float2);
printf("%f\n", float2);
On a side note, if you're confused about redirection, printf is a modified version of fprintf. Printing to the console is just so common there's a function for it. Saying printf("%f\n", float1) is the same as fprintf(stdout, "%f\n", float1). Same goes for scanf vs. fscanf. Redirecting output allows you to keep your printf statements in your code, yet still print to a file. Else you would have manually change them all to fprintf which would be a pain.

Related

Program accepting input but does not give output

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
printf("enter the 2 numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("the sum is : %d ",c);
return(0);
}
this is a simple program to add 2 numbers.
my program would let me input the value..but it would not print the sum ,nor would it print the next line.
it would run till the scanf() and as i press enter, it would jst exit the program.
can you please tell me whats wrong. I am a beginner programmer...
There are two things you should think of here.
End printouts with a newline character, because stdout is often line buffered. Do printf("the sum is : %d \n",c); instead. Or call fflush(stdout); expliticly after the printout. This will ensure everything gets printed.
Add some input code in the end. Like an extra scanf("%d", &a); This is basically a small hack to prevent the window from closing before you can see the final output. Another alternative is to add sleep(3); to sleep for 3 seconds. A third alternative here is to see if there are some settings that controls the closing of the window in your IDE.
Your program works correctly, but it exits right after printing the output, giving you no time to look at it.
Consider adding some input before return(0);, such as 2 getchar(); calls. You need 2, because the first character read will be the \n that you typed after the numbers.

While loop to validate input is a number in C? [duplicate]

This question already has an answer here:
Why inside a loop, scanf() with %d does not wait for the user input in case it received an invalid input previously?
(1 answer)
Closed 5 years ago.
So today I am trying to find a way to check if this input is a number. I cant find a way to make this code work so far. If I enter a number, the while loops ends, which is my intention. But, if I enter anything else, the code SHOULD print the printf once and then reprompt me for input (via the scanf statement back at the top of loop). But it instead prints the printf statement infinitely. How can I fix this?
float num1;
while (scanf("%f",&num1)==0)
{
printf("Invalid input. Please enter a number: ");
}
If scanf fails to preform the requested conversion, it will leave the input stream unchanged. So while your check is correct, you need to clean the input stream of the erroneous input before re-attempting to read a number again.
You can do it with scanf itself and and the input suppression modifier:
float num1;
while (scanf("%f",&num1)==0)
{
printf("Invalid input. Please enter a number: ");
scanf("%*s");
}
%*s will instruct scanf to parse the input as though it's attempting to convert a string of characters (removing characters in the process from the stream), but thanks to the asterisk, it won't attempt to write it anywhere.

Input reading using scanf hangs

I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main() {
int num;
printf("enter a number: ");
scanf("%i\n", &num);
for(int n = 1; n < num + 1; n++){
printf("%i\n", n);
}
return 0;
}
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
Remove the \n from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf() will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf() call.
Generally, scanf() is considered bad for input reading. So, considering using fgets() and parsing the input using sscanf().
See: Why does everyone say not to use scanf? What should I use instead?

gets() not working as expected

So first of all I would like to paste my code in here for future references:
int dayNum = 0;
printf("\n\nEnter your date(1 - 30/31): ");
scanf("%d\n", &dayNum);
printf("\n\nEnter your note:");
char note[10000];
gets(note);
printf("%s", note);
The code is I think self-explanatory and easy-to-understand. However, here is a quick and short explanation from my side. This code just gets an integer input and stores it into a variable and then gets ready to take a string as an input and print it out to the console.
What I expect:
I expect the code to run like this:
Enter your date(1 - 30/31): <my_input>
Enter your note: <my_long_note>
<my_long_note> //prints my note that I wrote above
What is happening:
But, what is happening right now is like this(abnormal):
Enter your date(1 - 30/31): <my_input>
<my_long_note> //this is an input
Enter your note: <my_long_note> //this is an output
As you can see, it takes my note before printing out Enter your note:.
Can someone tell me why is that happening? I am not quite sure of what did I do wrong in there.
You need to flush your output stream.. That means including a \n in the printf, or by manually calling fflush(stdout).
Ah, so after another search, I found this:
Using scanf("%d%*c", &a). The %*c term causes scanf to read in one character (the newline) but the asterisk causes the value to be discarded.
So my final code became this:
int dayNum = 0;
printf("\n\nEnter your date(1 - 30/31): ");
scanf("%d%*c", &dayNum);
printf("\n\nEnter your note:");
char note[10000];
gets(note);
printf("%s", note);
And, it worked.

My C code is not responding after scanf statement

I have wrote a set of code that scan in values and use them to test out central limit theorem. However when I run my program after I input all the values using scanf my program is not proceeding to the next lines of code : the problem looks like this:
printf("*** DEMONSTRATION OF CENTRAL LIMIT THEOREM ***");
printf("Enter parameters for the distribution [a b] ==> ");
scanf("%f %f",&a,&b);
printf("Enter distribution to display [1=data, 2=mean] ==> ");
scanf("%d",&option);
printf("Enter number in each group ==> ");
scanf("%d",&group);
printf("Enter number of samples of groups ==> ");
scanf("%f",&times);
printf("are we here yet");
after these printf and scanf the program starts to do the calculations. But When I run the program after I compile(successfully). It seems my code is stuck after the scanf("%f",&times);
the line "are we here yet" never gets printed, meaning the program did not get past the scanf. I have not done much C programming this seemed really strange to me can someone figure out why the program is not excuting past the line scanf("%f",&times); I really appericate it
Input/output at the terminal is line-buffered in C, and output is not going to display until you output a newline character, or you call fflush(stdout), or your program terminates normally and all the buffers are flushed anyway. Change:
printf("are we here yet");
to:
printf("are we here yet\n");
or:
printf("are we here yet");
fflush(stdout);
and you should see your output.

Resources