This question already has an answer here:
Why no output on console on signal handling?
(1 answer)
Closed 4 years ago.
I want to test if different loops are active, so I have a print statement that repeats every 500ms in each loop, however the print statement does not print every 500ms, it waits until the loop is finished and then prints everything at once instead of periodically.
How do I get my program to print to the terminal periodically?
I'm a student so my knowledge of SDL is pretty limited so thorough explanation would be appreciated.
int main(void)
{
int i = 0;
while(i<10)
{
printf("While loop active.\t"); i++;
SDL_Delay(500);
}
return 0;
}
P.S. I saw that duplicate question but I disagree as his question suggests an issue with 'signal handling' which I know nothing about so when asking this question, I didn't think his question would have the same answer as mine. I accept the answers given are the same though.
You are probably not flushing stdout. Add a newline to the printf call and you should be OK:
printf("While loop active.\n");
/* Here ------------------^ */
Or if keeping \t is essential:
printf("While loop active.\t");
fflush(stdout);
Credit to #lurker for extra information.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know why, but my code is not taking input.... where did i do mistake???
After running it just prints this:
Type your input (press enter to save and exit).🙂️
Done, your file is saved successfully🤩️
My code is:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fpp;
char Entry;
char sid;
fpp = fopen("sid","w");
if (fpp == NULL)
{
printf("Sorry🙁️ file not created\n");
exit(0);
}
printf("Type your input (press enter to save and exit).🙂️\n");
while (1)
{
putc(Entry,fpp);
if(Entry =='\n')
break;
}
printf("Done, your file is saved succesfully🤩️\n");
fclose(fpp);
return 0;
}
yes, guys I used scanf() instead of putc(). My online tutor said me to write this...
while((ch=getchar())!='\n')
{
putc(Entry,fpp);
}
and I used that.... but now I used this code and it worked.
while (1)
{
scanf("%c",&Entry);
if(Entry =='\n')
break;
}
In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell.
Other commenters provided wise input, which I however do not see as immeditate parts of the solution.
Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:
Why does any input you provide not have any influence on program behavior?
Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments
putc is used for output, not effective input.
I.e. with no input reading functions called there is no input.
Why does Done, your file is saved successfully🤩️ occur at all? It should only occur after '\n' input.
Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:
... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.
I.e. unconditional immediate break, end of loop, output. End of program.
This question already has answers here:
printf just before a delay doesn't work in C
(5 answers)
Closed 5 years ago.
Why below function is not printing "Just kidding!".
void justCheck() {
printf("Just kidding!");
while (1) {
}
}
while this is printing "Justing Kidding!" followed by non stopping "Just Kidding inside loop!".
void justCheck() {
printf("Just kidding!\n");
while (1) {
printf("Justing Kidding inside loop!\n");
}
}
can anyone please explain the logic?
Your first example
printf("Just kidding!");
The output is buffered and therefore not displayed
In the second example
printf("Just kidding!\n");
The \n at the end will flush the buffer and therefore the string will be displayed.
In the first example before the while loop insert fflush(stdout);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have some homework about a "hangman" game and initially I need to show the rules on the console for only 5 seconds after which it must dissappear and the game will start. How can I achieve this in C?
For example:
"You can only try 5 times"
After 5 seconds this should dissappear and the game will start.
I am using the DEV-C console and have researched the time.h library but the part I'm most stuck at is how to make the text dissappear.
You could do something like this using sleep if you don't need to do anything during those 5 seconds:
int main()
{
printf("You can only try 5 times");
sleep(5);
// Start game
return 0;
}
For dissappearing text you alluded to in comments you can use carriage returns \r:
printf("\rI will overwrite the previous text!");
But this only works if you're overwriting with a string longer than what's already printed. You can print a blank line first to 'erase' it to get around this.
Finally, as most output streams are buffered, your text may not print without the newline \n character straight away, to get around this you can use fflush(stdout) so your final implementation might look something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("\rYou can only try 5 times");
fflush(stdout);
sleep(5);
printf("\r ");
fflush(stdout);
printf("\rThe game will now begin.");
fflush(stdout);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm working on my study for passing class. I've done everything, but my foor loop doesn't work well, what should i do for fixing it?
inside of 4th for loop, first loop (a) starts from 9, second loop (b) starts from 3. Why?
Code:
#include <stdio.h>
#include <conio.h>
int main () {
int i,j,a,b;
for(a=0; a<=10; a++){
for(b=0; b<=10; b++){
for(i=0; i<=3; i++){
for(j=0; j<=3; j++){
printf("a: %d -- b: %d -- i: %d -- j: %d \n",a,b,i,j);
}//--j for--
}//--i for--
}// --b for--
}// --a for--
getch ();
return 0;
}
Output:
Your program is fine, but it prints 1936 lines of output very quickly, so you only see the last 25 or so and scrolling back, you cannot get back to the beginning of the output because the Windows terminal seems to have very limited backscroll capability, maybe 150 lines.
You can try and increase the backscroll capability in the Windows settings pages (I have not used Windows for many years, I do not know where to do that nor if it can be done).
There are other methods to convince yourself that the code is fine:
redirect the output to a file and load that file: open a terminal window and type myprogram > output.txt in the terminal. You agree going to need to hit the keyboard because of the getch() at the end of the main function. As a matter of fact, you could remove this line altogether. As mentioned by manetsus, redirection can be forced from within the program by calling freopen("output.txt", "w", stdout); before the first for loop.
pipe the output through more. Again open a terminal window and type myprogram | less. The call to getch() is counterproductive for this too.
move the getch() to the end of the second loop (just before }// --b for--). The program will produce 16 lines at a time and wait for a key.
You are seeing the output in the console and every console provides a limited buffer memory.
So, the previous outputs were generated, but they are truncated in the consoled you showed.
You can get the full output in a file named output.txt by adding the following line as the first line of your main() function:
freopen("output.txt","w",stdout);
Or, from linux console, you can write the following command as #BLUEPIXY said in the comment:
./a.out > out.txt
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
On running the following script, I get segmentation fault. The output consists of "here 5a". But nothing beyond it. Any suggestions on what might be going wrong?
if(model==1)
fptr3=fopen("poisson.pro","r");
if(model==2)
fptr3=fopen("jtt.pro","r");
if(model==3)
fptr3=fopen("estimate.pro","r");
printf ("here 5a\n");
for (i=0;i<20;i++)
fscanf(fptr3,"%lf", &freq[i]);
printf ("here 5ai\n");
for (i=0;i<20;i++)
{
printf ("here 5b\n");
for (j=0;j<20;j++)
{
printf ("here 5c\n");
fscanf(fptr3,"%lf", &prob[i][j]);
if(model==3)
prob[i][j]=prob[i][j]/10000.0;
}
}
UPDATE
double freq[20], prob[20][20];
Are you sure this code is called and "model" is either 1, 2, or 3? If not, fptr3 would not be set, so if you try to do anything with it you'd be in trouble.
Try this instead?:
void modeltest (int model)
{
FILE *fptr3 = NULL;
int i = 0;
int j = 0;
double freq[20], prob[20][20];
if(model==1) fptr3=fopen("poisson.pro","r");
if(model==2) fptr3=fopen("jtt.pro","r");
if(model==3) fptr3=fopen("estimate.pro","r");
printf ("here 5a\n");
if (fptr3 != NULL) {
for (i=0;i<20;i++) fscanf(fptr3,"%lf", &freq[i]);
printf ("here 5ai\n");
for (i=0;i<20;i++) {
printf ("here 5b\n");
for (j=0;j<20;j++) {
printf ("here 5c\n");
fscanf(fptr3,"%lf", &prob[i][j]);
if(model==3) prob[i][j]=prob[i][j]/10000.0;
}
}
}
else {
printf ("fptr3 is NULL!\n");
}
}
An additonal note. Please, please, please consider consistent brace styles! :)
Is this C# ???? wooow no kidding you are getting segfaults...
Do you know what return codes are for ???
You really need to improve your code before asking for this kind of help....this is really unreadable, and ugly code...
Start adding some check to your file pointers
if(1==model)
{
fptr3=fopen("poisson.pro","r");
if(null == fptr3)
{
perror("Fopen failed");
}
...
valgrind is the sovereign remedy for segfaults in C. It finds errors before the segfault and tells you exactly what you did wrong. For maximum benefit, compile with debugging symbols turned on.
I agree with Daniel, you need to be checking your return values and pointers.
I also agree that you need to do a better job blocking and formatting your code. Supposing that you are the only one that ever reads it, in a couple of weeks even you will have forgotten enough that you won't be able to follow it.
Towards Matthews point, you could try deleting everything starting with the line:
printf ("here 5ai\n");
Of course, only bother with this after checking the file pointer.
Another crazy idea would be stepping through it with a debugger. I know it's hard to use a debugger when you have such great debugging options as the printf statement, but sometimes a debugger can be a huge time saver (for example, when you keep getting segfault). Also, learning how to use the debugger for the platform you are developing on is one of the things that separates good coders from the pack of average hacks. (FYI, you could even try looking at your core in the debugger and maybe see exactly where the problem is. (Hint: if you don't have a core file try 'man ulimit'.))
If you are going to insist on using printf as your only means of debug, then call flush right after each use.
What's freq defined as? What's prob defined as?
Is the first for loop meant to only run fscanf for freq? Or is it supposed to encompass the entire block?
EDIT: Rationale - If all you see is 5a, then it's faulting in the fscanf on freq. You may have allocated too little space for freq, or used an incorrect type.
Check the return code and print if needed the error code of your files with perror() (see. daniel answer). Calling fscanf() with a FILE * to NULL, will surely do a segfault.
As you specified %lf, freq must be an array of at least 20 doubles, not just float.
See man ffrintf().
use a debugger instead of printf().
You should post the contents of the beginning of whatever file is getting opened since it's crashing in fscanf. Either the file doesn't exist, or it's malformed when stacked against to how you are trying to parse it.