how to clear my previous output in Linux terminal using C? - c

This is my sample program for simulating a Loading progress. The only problem I'm facing right now is clearing my previous output of "Loading: %i"
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < 5; ++i) {
j = i;
printf("Loading: %i%%", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}

if you print \r instead of \n using printf, then it will return to the beginning of the same line instead of the next line. Then you can re-print the new status on top of the old line.

Anyway here is the correct code. Thanks to Mobius and Dmitri.
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < TIMELIMIT; ++i) {
j = i;
printf("Loading: %i%%", ++j);
printf("\r");
fflush(stdout);
sleep(1);
}
return(0);
}

I think the answer to this question is system("reset").
This is the command you're looking for
reset
this command clears your whole terminal.
and your code go like this,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
int i,j;
for(int i = 0, j; i < 5; ++i) {
j = i;
system("reset");
printf("Loading: %i%%\n", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}

Related

Printing Staircase pattern using only one loop

enter image description here
I thought about printing this shape only using one loop but I didn't make it and I want to know if it's possible and if it's possible what should I do ?
Here is my code:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 16; i++) {
if (i % 4 == 0) {
printf("\n");
printf(" ");
}
else
printf("*");
}
}
Having only allowing one for loop for the task seems like an artificial constraint, but with that in mind, utilizing some string functions would allow you to perform that task. Following is a snippet of code utilizing just one for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 24
int main()
{
char gap[MAX];
char * stair = "***";
strcpy(gap, "");
for (int i = 0; i < (MAX / 3) - 1; i++)
{
printf("%s%s\n", gap, stair);
strcat(gap, " ");
}
return 0;
}
Following was the sample output at the terminal.
#Dev:~/C_Programs/Console/Stairs/bin/Release$ ./Stairs
***
***
***
***
***
***
***
See if that meets the spirit of your project.

CS50 Readability, How to print the total number of letters?

Hey I've started on the readability problem for cs50 and I'm stuck on step two. I'm sure it's a simple fix but I've already spent an hour or so racking my brain on it to no avail. Basically I just want to print off the total number of letters but when I print off the integer it prints off the entire sequence.
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
string s = get_string("Text:");
printf("%s\n", s);
for (int i = 0; i < strlen(s); i++)
{
printf("%i\n", i);
}
}
which gives me this
Text:hello
hello
0
1
2
3
4
This will do the job for you.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void)
{
string s = get_string("Text:");
int length = 0;
length = strlen(s);
printf("%i\n", length);
}
However, this will take spaces into account as well. Only counting the letters, you have to build for loop to check every single character if it is alphabetic or not. There are built in functions for this. Please do research.

How to redirect stdout with '\r' printed on the same line?

Check below the code snippet of using the character \r:
#include <stdio.h>
#include <unistd.h>
int main() {
for(int i=1; i<=100; i++) {
printf("%d%%\r", i);
fflush(stdout);
usleep(1e4);
}
printf("\n");
return 0;
}
The output of this program on the console has only 1 line:
$ ./main
100%
However if the output is redirected to a file ./main > main.log, the file has 100 lines:
1%
2%
3%
...
100%
Question:
How do I keep only the last line of output to the log file?
I am assuming this is part of a larger program where the "percentage" may or may not be 100% (otherwise, you could just print 100% right away).
So you could check if the output goes to a terminal using isatty. If not, print the "last percentage" value:
int main(void)
{
int b = isatty(STDOUT_FILENO);
int i;
for(i=1; i<=100; i++) {
if (b) printf("%d%%\r", i);
fflush(stdout);
usleep(1e4);
}
if (!b) printf("%d%%\r", i-1);
printf("\n");
return 0;
}

How to print 0 to 9 digits without using any digit in our code?

Recently, I found a problem on hackerearth that is how to print 726 without any digits. This is the link to question :https://www.hackerearth.com/challenge/college/r1/golf/726/
I have solved this with this:
#include <stdio.h>
main()
{
printf("%d",('\e'*'\e')-('\r'-'\n'));
}
but since I found this question interesting, I would love to print every single digit without use of any digit. So, I want to print 0 to 9 all in a new line without using any digit with the help of unicodes. How could I do that?
#include <stdio.h>
int main(void){
for(int n = !&n; snprintf(NULL, !&n, "%d", n)==!!&n; printf("%d\n", n++));
}
#include <stdio.h>
#define ZERO ('a' - 'a')
#define ONE ((int)(sizeof (char)))
#define TWO (ONE + ONE)
#define THREE (TWO + ONE)
#define SIX (TWO * THREE)
#define SEVEN (SIX + ONE)
int main(void) {
printf("%d%d%d\n", SEVEN, TWO, SIX);
/* Assume C99 and implicit return 0; */
}
There's countless of ways to write obscure, nonsense C code. One example:
#include <stdio.h>
int main (void)
{
for(size_t i=!"A string."; i<sizeof("A string."); i++)
printf("%zu ", i);
}
Or, more creatively, in pure standard C:
??= include"stdio.h"
#define wtf(x,...) x%:%: nt ma ##x %:%:n(vo ##x %:%:d) ??<for(x %:%:nt x =(+ __LINE__??!??!\
+__LINE__) +- !!"WTF"; x<=__LINE__; +i++)??<\
pr ##x %:%:nt %:%:f((char[])<%'%','d',' ',!"WTF"??>,-+-x);%>??>
wtf(i)
#include <stdio.h>
void main()
{
char ch='/';
while(ch!=':')
{
if(isdigit(ch))
{
printf("%c\n ",ch);
}
ch++;
}
}
You can check the values of ASCII code an use the char with %d placeholder for extract the number.
E.g. :
#include <stdio.h>
int main(int argc, char **argv)
{
char a='H';
printf("%d",a);
return 0;
}

array of string output is a clover symbol

No matter what the 5th input the output was clover symbol, program purpose was to align right the inputs:
EDIT
im not using scanf(%[\^n],a[i]), the output was horrible, using gets instead
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char a[6][50];
int i;
for (i=1;i<=5;i++)
{
printf("insert name%d :\n",i);
gets(a[i]);
}
for (i=1;i<=5;i++)
{
printf("%d%25s\n",i,a[i]);
}
return 0;
}
Example output:
1 far cry
2 iron man
3 new super mario
4 program
5 "clover"
Using your code, I got strange chars on output as you mentioned.
So, I made some fixes on your code and now I think it works properly.
#include <unistd.h>
int main(void)
{
char a[6][50];
int i, r;
for (i = 1; i <= 5; i++) {
printf("insert name%d :\n", i);
r = read(STDIN_FILENO, a[i], 49);
a[i][r] = '\0';
}
for (i = 1; i <= 5; i++) {
printf("%d%25s\n", i, a[i]);
}
return 0;
}
Avoid to use deprecated functions in your code.
You need to ensure that after reading from stdin, your buffer is going to get the null terminator right after the last position written in buffer.

Resources