Please refer to the following code snippet, I will be referring to the line numbers on it:
https://gist.github.com/wilbertcr/474c6a13e377dc8ce51a
As you can see on line 172-200, I created a modified version of the original back_over function, which just moves the pointer without erasing the character that's moving over.
Outside of the function in_process, and as a global variable, I created int verase, see line 1, which I use on line 82 to indicate CTRL+H has been pressed(ch == tp->tty_termios.c_cc[VERASED]).
My idea is to use this as a flag so I can catch the next character, which should be the number of spaces I need to back_over, and do that by calling back_over the number of times indicated by that next character.
The problem I am having is that I don't know how to turn that next character into its corresponding int so that I can use it on the for loop. Lines 27-38 show a failed attempt to do that, however, no matter how small the key I press(I've tried 1 and 2 and 3), it always takes me to the beginning of the line(luckily back_over doesn't go beyond a line break--see line 185).
It seems like the for loop is being executed more than the it should, which I suspect is happening because "number" is not really 1 or 2 or 3 but something else, something the for loop is interpreting as much bigger than the number I am pressing in the keyboard. I would appreciate some help on how can I turn it into an int.
It turned out it was just a matter of doing:
ch=ch-'0'
Related
After spending more than few days on debugging, I can't find the reason why my code below creates additional spaces when printing out the error check ("expecting or missing"). For example, when I run each of these line of symbols one at a time in order.
The first two works fine, but the third line of input will error check at the wrong position. But the symbol that its expecting is correct (<). When I remove the call to the reset function in the bottom of my code, the error check of the third line of input will be at the correct position but the symbol that its expecting is incorrect: { . Most likely because the stack hasn't reseted from the previous line of inputs.
I made print statements on the values of the increments for printing spaces and the size of the char array itself, but the numbers are correct. The culprit might be the reset function I made, but the character resets fine. Only problem is that the output is creating additional spaces when printing out the error check, which leads to displaying the error check at the wrong position.
You need to reset check1 and check2 to false within your while loop. Other wise when running the third case after the second it includes extra white space resulting in the wrong offset.
I am a beginner of C language. I was recently writing a program to print a histogram of the number of instances of a character in an input. Printing the histogram horizontally is easy, but vertically is a challenge.
Please have a look at the following code:
#include <stdio.h>
int main()
{
/*THIS IS JUST A SAMPLE OF WHAT I WANT TO ASK*/
int occurrence = 5;
int i;
for (i=0;i<occurrence;i++){
printf("\t*\n");
}
}
For an example, say any letter occurs 5 times. So I have set the occurrence to 5. And I am printing the bar in the form of asterisks. Now through this code, I am able to print an histogram containing 5 asterisks. But the thing is if I want to print other elements, like the x and y axis, the code creates a \n character. So if I write the code to print other elements, they start printing from the next line. So I figured out something else.
Now read this code:
#include <stdio.h>
int main()
{
/*THIS IS JUST A SAMPLE OF WHAT I WANT TO ASK*/
int instances = 5;
int i;
for (i=0;i<instances;i++){
printf("\t*\t\t\t\t\t\t\t\t\t");
}
}
Now what I did is depending on the size of the output screen, I created 9 tab characters so that the next asterisk moves on to the next line without printing any \n character.
Now the main question: IS THERE ANY OTHER WAY TO CREATE INVISIBLE SPACES UNTIL THE NEXT THING TO BE PRINTED MOVES ON TO THE NEXT LINE?
This question might be stupid but if there is a solution, it will be best for me.
NOTE: If there is no such method of creating blank spaces then please suggest a good way to create a vertical histogram. If someone wants an improvisation in the question, I am ready to do it.
Thanks for the help!
Outputs::
If I use the first code and I make other chart elements using printf statements, this is the output::
Now can you see that the bar made of asterisks is not aligned with the x and y axis. This happens due to the \n character.
Instead of hacking around with spaces yourself, you might want to look into a library that handles all that graphical stuff for you. For example, ncurses is a pretty decent library to do pseudo-graphical output on a console. However, "ncurses" seems to be for Unix only, but there may be other libraries for Windows.
If using a library is not an option, I'd suggest to work with a 2-dimensional character buffer (that you treat like a bitmap) instead of writing things directly to the console. It's a lot less "fiddling around". Just watch out to truncate your buffer line size to the console line size before printing, in order to avoid automatic line breaks where you don't want them.
If you do not want to use curses library, for example if you have found a printing terminal in a museum and want that your program can work on it, you have to reverse the problem.
You must print line by line if you do not use a graphic library. So your program could look like :
compute the occurence of characters
compute the maximum occurence
for each line from max occurence to 0
compute a line for every character printing a space (not reached)
for each character
if the occurence is greater than the line index put a mark at correct place in the line
print the line
That's the algo, actual coding is left as exercice for the reader :-)
You might want to think about having a two dimensional array. Start by filling it with spaces. Then replace the spaces with the character you want to print at the correct index. Using two for loops, traverse the array in order to print. Print a new line at the end of the row. Changing the order of the index changes a vertical/horizontal print. Negatives are
having to keep the whole thing in memory.
Needing multiple passes. One to set the characters and another to print.
I'm making a very simple console text editor. In order to move the cursor to the end of the previous line when I backspace at the beginning of a line, I need to read an already printed line from the console, then get its length. How would I go about making this work?
There is no way to do this using scanf() and printf(); the program would need to keep track of what it prints. However, it is also impossible to edit previously printed screen lines using only the standard I/O functions - in order to do that, you'd need to use e.g. ncurses.
Store every element you print in the editor in an char array, because you may have to go back all way up to start or in the middle, not only one line up.
I'm writing a business application in C in which I'm keeping track of a timer by writing the value to a temporary file. The counter starts at 96, and counts backwards all the way to 0. The logic works fine, except when it switches over from 10 to 9, the 9 only overwrites the 1, so instead of 10 -> 9, the value goes 10 -> 90, causing the countdown to start back from this point. Is there a way I can have my application write the value successfully?
I'm using the following two functions to control output to the file"
fprintf(fileFd, "%d" , counter);
rewind(fileFd);
I print the value to my file, then move the access point back to the head of the file for the next entry. Any advice on how this can be done? Thanks, all.
EDIT: Thank you all for the suggestions. I tried the methods suggested below, by calling %02d to write to two place values, and for some reason that completely broke the application. The loops won't happen at all.....then when I recompile, it still won't work until I delete my temp file and create a new one. Now, when writing to the file, it tells me at the bottom (this is a UNIX system) the filename, [Incomplete last line], 1 line 2 characters. Would this make any kind of difference to specifying the number of places to write to?
fprintf(fileFd, "%02d", counter);
Check with:
fprintf(fileFd, "%02d" , counter);
write to the file taking up certain number of digits, and fill unused digit with 0.
fprintf(fileFd, "%07d", counter);
The above code will allow time values upto 7 digits.
Its a microsoft interview question.
Read last n lines of file using C (precisely)
Well there could be so many ways to achieve this , few of them could be :
-> Simplest of all, in first pass , count the number of lines in the file and in second pass display the last n lines.
-> Or may be maintain a doubly linked-list for every line and display the last n lines by back traversing the linkedlist till nth last node.
-> Implement something of sort tail -n fname
-> In order to optimize it more we can have double pointer with length as n and every line stored dynamically in a round robin fashion till we reach the end of file.
for example if there are 10 lines in file and want to read last 3 lines. then we could create a array of buffer as buf[3][] and at run time would keep on mallocing and freeing the buffer in circular way till we reach the last line and keep a counter to know the current index of array.
Can anyone please help me with more optimized solution or atleast guide me if any of the above approaches can help me get the correct answer or any other popular approach/method for such kind of questions.
You can use a queue and to store the last n lines seen in this queue. When you see the eof just print the queue.
Another way is reading a blocks of 1024 bytes from the end of file towards the beginning. Stop when you find n \n characters and print out the last n lines.
You can have two file pointers initially pointing to beginning of file.
Keep on incrementing first pointer till it find '\n' character also stores the instance of file pointer when it find '\n'.
Once it find (n+1)th '\n',assign first stored instance of file pointer which we previously saved,to second file pointer.Keep on doing the same till EOF.
So when first file pointer is on EOF,second will be on n '\n' back.Then print all characters from second file pointer to EOF.
So this is solution which can print last n lines in file in single pass.
How about using memory mapped file and scan the file from backward? This eliminates the hard work of updating the buffer window each time every time if the lines happened to be longer than your buffer space. Then, when you found a \n, push the position into a stack. This works in O(L) where L is the number of characters to output. So there is nothing really better than that is it?