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.
Related
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 2 years ago.
Improve this question
When I used the switch statement in the C language and ran it,the output contains two linebreak after it .
I don't know why and just want to get the end result.
Please tell me why and how to deal with it.
Thank you very much !
#include<stdio.h>
int main(){
int i;
printf("Please enter an integer:");
scanf("%d",&i);
switch(i){
case 1:printf("%d---A\n",i); break;
case 2:printf("%d---B\n",i); break;
case 3:printf("%d---C\n",i); break;
case 4:printf("%d---D\n",i); break;
default : printf("%d---E\n",i); break;
}
return 0;
}
when I input 1 will show:
The following code does not output a newline:
#include <stdio.h>
int main(void)
{
printf("Hello world");
}
Now, if my command shell did not start a new line, the prompt would be
Hello worldC:\TEMP>
So the shell starts a new line, and the console looks like this:
Hello world
C:\TEMP>
But if I output a newline in my program, there will be blank line:
Hello world
C:\TEMP>
The command shell does not know if the final output of the program was a newline or not, without exploring the console output. So for simplicity, it just starts a new line. It's not a bug. If you don't want a blank line, don't output a newline.
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.
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 6 years ago.
Improve this question
I am trying to read from a fifo and i am not understanding it's behaviour.
This is the write side, write.c:
for(int i = 1;i<argc;i++){
if(write(fifoFd,argv[i],strlen(argv[i])) <= 0)
perror("Error writing");
}
And this is the read side, read.c:
char buf[1024];
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
First i start read.c ./read then i execute ./write
If i execute write like this ./write backup *.txt sometimes i get what i expected, i.e, backupexample1.txtexample2.txt.
But sometimes i only get example1.txtexample2.txt and i am not understanding this, where is "backup"?
Your code:
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
You loop, each time through the loop you overwrite the buffer, and then you print the buffer. So, sometimes, you read "backup" followed by "example1.txtexample2.txt" (which overwrites "backup"), other times you read the whole lot at once in a single read.
If you change the loop to read into the unpopulated portion of the buffer, it will behave consistently:
int read = 0;
while(read != 1024){
int b = read(fifoFd,buf+read,1024-read);
if(b<=0) break;
read += b;
}
printf("%s\n",buf);
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 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
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here