What is ^X in in Linux? - c

For my homework assignment, I have to make sure that my output matches the solution output 100% or I don't get credit.
My output is:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34
The solution output is:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34
They look similar, however when I use diff mine.txt solution.txt, I get a difference.
I used cat -tev mine.txt and cat -tev solution.txt to find the difference, and I found that mine looks like:
hw05-data-10.txt^X : min = 5, max = 90, mean = 51.23, variance = 618.34$
and the solution looks like:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34$
What is ^X? I've tried looking around but I can't find the answer. How can I remove this from my output? It's a C program.

^X is the CTRL-X character, made visible by the -v flag of cat.
If you want to get rid of it, you can pass the contents through something like:
tr -d '[:cntrl:]'
See the following transcript for an example (the ^X is inserted in bash with CTRL-VCTRL-X):
pax> echo '123^X456' | cat -v
123^X456
pax> echo '123^X456' | tr -d '[:cntrl:]' | cat -v
123456
If you want to remove it at the source rather than filtering after the event, you need to investigate the program that's creating your mine.txt file. It will be the one inserting the rogue character.

According to the man page of cat command,
-v flag: Display non-printing characters so they are visible. Control characters print as ^X for control-X; the delete character (octal 0177) prints as ^?. Non-ASCII characters (with the high bit set) are printed as M- (for meta) followed by the character for the low 7 bits
-e flag: Display non-printing characters (see the -v option), and display a dollar sign (`$') at the end of each line.
Therefore, the answer of ^X is a Control Character. To remove from output, remove the -v and -e flag from your cat command if possible. If can't, you have to investigate your C program: why it's generating this control character.

^X is ASCII CAN, cancel control character. Normally it is not used by the linux tty driver on output, so you will not see it when output to the terminal. If you use it on input, linux tty driver will interpret it as the STOP character and will send a SIGSTOP signal to the foreground process, causing it to stop execution until it receives a SIGSTART signal. The character you mention is probably somewhat inserted in the format string you used for the printf(3) call. (you have not shown your code snippet in the question)
The ^X character is also used in some window environments to Cut the selected text, so probably you inserted it at some undesired place in the program source code.

Related

Right aligning using printf_P in C

I am attempting to get an output of:
Score:
0
but my output keeps coming out like
Score: 0
this is what I have implemented:
move_cursor(30,4);
printf_P(PSTR("Score : %8d\n"), get_score());
move_cursor(37, 8);
we are writing the score in Putty, from AVR to serial.
What am I doing wrong?
Q: If you want "0" on a separate line ... then shouldn't you put a matching `\n' in your format statement?
Q: If You want it right-aligned at column 6, then shouldn't your format statement be %6?
EXAMPLE: printf_P(PSTR("Score :\n%6d\n"), get_score());
PS:
As you're probably aware, "printf_P()" isn't standard C; it's AVR-specific.

Bash formatting text file into columns

I have a text file with data in it which is set up like a table, but separated with commas, eg:
Name, Age, FavColor, Address
Bob, 18, blue, 1 Smith Street
Julie, 17, yellow, 4 John Street
Firstly I have tried using a for loop, and placing each 'column' with all its values into a separate array.
eg/ 'nameArray' would contain bob, julie.
Here is the code from my actual script, there is 12 columns hence why c should not be greater than 12.
declare -A Array
for((c = 1; c <= 12; c++))
{
for((i = 1; i <= $total_lines; i++))
{
record=$(cat $FILE | awk -F "," 'NR=='$i'{print $'$c';exit}'| tr -d ,)
Array[$c,$i]=$record
}
}
From here I then use the 'printf' function to format each array and print them as columns. The issue with this is that I have more than 3 arrays, in my actual code they're all in the same 'printf' line. Which I don't like and I know it is a silly way to do it.
for ((i = 1; i <= $total_lines; i++))
{
printf "%0s %-10s %-10s...etc \n" "${Array[1,$i]}" "${Array[2,$i]}" "${Array[3,$i]}" ...etc
}
This does however give me the desired output, see image below:
I would like to figure out how to do this another way that doesn't require a massive print statement. Also the first time I call the for loop I get an error with 'awk'.
Any advice would be appreciated, I have looked through multiple threads and posts to try and find a suitable solution but haven't found something that would be useful.
Try the column command like
column -t -s','
This is what I can get quickly. See the man page for details.

AutoHotkey's Loop (read file contents) issues related to "+" symbol

Referring to Loop (read file contents), a quite strange thing happens every time I use a code like this one to run a script:
^+k::
{
Gosub, MySub
}
Return
MySub:
{
Send, +{Enter}
Loop, read, C:\MyFile.txt
{
temp = %A_LoopReadLine%
Send, %temp%
Send, +{Enter}
}
}
Return
MyFile.txt is a simple text file where sometimes the "plus" symbol (+) is used together with normal letters and numbers.
Despite of this, however, what I see if I run the hotkey on an empty text file, either a Notepad or Microsoft Word blank sheet, is that every + is replaced by an underscore (_), an exclamation mark (!) or a question mark (?). I've seen an occurrence with a dollar symbol ($) replacement, too.
I tried to debug it printing on screen a message box with
MsgBox, %temp%
before sending text and it shows the original content of MyFile.txt perfectly.
Thus the issue should be on Send rather than on file reading.
The content of my file is something like this (repeated for about 20 rows more):
+---------------------------------
120001267381 ~ TEXT 0 10/20/18 VARIABLE word text -> numbers: 17,000 x 108.99 | 109.26 x 15,000 /// number = +5.500% some text
+---------------------------------
120001267381 ~ TEXT 0 10/20/18 VARIABLE word text -> numbers: 17,000 x 108.99 | 109.26 x 15,000 /// number = +5.500% some text
+---------------------------------
120001267381 ~ TEXT 0 10/20/18 VARIABLE word text -> numbers: 17,000 x 108.99 | 109.26 x 15,000 /// number = +5.500% some text
+---------------------------------
120001267381 ~ TEXT 0 10/20/18 VARIABLE word text -> numbers: 17,000 x 108.99 | 109.26 x 15,000 /// number = +5.500% some text
+---------------------------------
What can be the cause of this?
Found the answer: due to the fact that + symbols read from my file are sent like pressing the Shift key, the output is amended by the pressing of such a key instead of sending the original symbol present in file.
In order to send the original content of my file without triggering special hotkeys, I have to use SendRaw instead of Send, like in this example:
^+k::
{
Gosub, MySub
}
Return
MySub:
{
Send, +{Enter}
Loop, read, C:\MyFile.txt
{
temp = %A_LoopReadLine%
SendRaw, %temp%
Send, +{Enter}
}
}
Return
Here's an updated version that pastes using CTRL-V instead of Send to "retype" rows of data:
^+k::
{
Gosub, MySub
}
Return
MySub:
{
Send, +{Enter}
Loop, read, C:\MyFile.txt
{
temp = %A_LoopReadLine%
Clipboard = %temp% ; Write to clipboard
Send, ^v+{enter} ; Paste from clipboard
Sleep 10
; Short delay so it doesn't try to paste again before the clipboard has changed
; This check can get a lot more complex, but just increase it if 10 doesn't work
}
}
Return

Handling CR line endings in Lua

I'm trying to read a file with CR line endings using the file:read method which seems to be acting up for some reason. The file contents look like this:
ABCDEFGH
12345
##
6789
I want it to behave consistently with all types of line endings. Every time I try to read the file, it returns the last line in the file concatenated with the any trailing characters from the previous lines that have a greater position than the position of the last character in the last line. Here's what I mean:
> file=io.open("test.lua", "rb")
> function re_read(openFile)
openFile:seek("set");
return openFile:read("*a");
end
> =re_read(file) -- With CR
67895FGH
> =re_read(file) -- With CRLF
ABCDEFGH
12345
##
6789
> =re_read(file) -- with LF
ABCDEFGH
12345
##
6789
>
As you can see, the string being returned is the last string plus 5 in the previous line and plus FGH from the first line. Any lines shorter than the last line are skipped.
My goal is to use the file:line() method to read the file line by line. My hope is that if a 'fix' for file:read is found then it can be applied to file:lines().
In the case with CR only, re_read actually works as expected: it returns the lines separated by CR. But when the interpreter displays it, it interprets the CR characters as "go back to the beginning of the line". So here is how the result changes line by line:
ABCDEFGH
12345FGH
##345FGH
67895FGH
EDIT: here it is character by character, with a "virtual cursor" (|).
|
A|
AB|
ABC|
ABCD|
ABCDEF|
ABCDEFGH|
|ABCDEFGH
1|BCDEFGH
12|CDEFGH
123|DEFGH
1234|EFGH
12345|FGH
|12345FGH
#|2345FGH
##|345FGH
|##345FGH
6|#345FGH
67|345FGH
678|45FGH
6789|5FGH
Proof:
> s = "ABCDEFGH\r12345\r##\r6789"
> =s
67895FGH
You could normalize your line endings with gsub then iterate over the product with gmatch.
local function cr_lines(s)
return s:gsub('\r\n?', '\n'):gmatch('(.-)\n')
end
local function cr_file_lines(filename)
local f = io.open(filename, 'rb')
local s = f:read('*a')
f:close()
return cr_lines(s)
end
for ln in cr_file_lines('test.txt') do
print(ln)
end

using hash (#) symbol to indicate input newline of command to a database linkedlist C program?

hi i am doing a C database program based on linear single linked list due to my college project.
the problem that i dont understand yet is the input and output program.
well basically the input of program is should be like this
INSERT
001 // indicates patient's number
John // indicates patient's name
M // indicates patient's gender
#
002
Mary
F
#
SELECT // which means print the list
* // indicates all the list to be print
#
EXIT
input is done and the following line is the outputs
Data is added.
#
Hospital ID : 001
Name : John
Gender : M
#
Hospital ID : 002
Name : Mary
Gender : F
#
that's all.
Here is what I've been doing (in description)
first line is gets for a command
then doing the while command is not exit and not the # symbol in char , i check the command if it's "INSERT or "SELECT" using strcmp, if its equal then
the next line is gets for the input that is used in the linkedlist command, where i give a flag in int that if the command is insert it will ask for 3 times , or SELECT if it's the * in char symbol with strcmp it will deleteall
so basically after it repeats (flag) times to ask for inputline (using gets) it will ask for command again (forgot to mention before that char[size]), if # is inputted
so to print the command in order i enqueue them in my queue for printingoutput
until EXIT is inputted , it keeps enqueue , otherwise it will dequeue until the queue is NULL ( put the queue front in char temp )
BUT the whole algorithm doesnt seem to work, since the program only works out correctly for INSERT function.
then i am going back to my C book and read about the macro , parameter and such and research the internet until i found the idea of defining # itself to be a command to ask for more command
BUT doesn't seem to quite understand how the thing works that way.
any idea of what i should be doing to get the proper input and output ?
using this kind of strcmp while and if , or define the # itself ?
thank you in advanced, any help would really be appreciated.
The hashtag # is not meaningful in the input of your program unless you wrote your program for it to be.
The hashtag for preprocessing directives like #include or #define is meaningful in a C source code file (or a C header file). All your program is getting as an input is characters, not commands.

Resources