How to scanning this line with fscanf? - scanf

Given file with the only line:
001005023000.0028
How can I scan this file (file.txt) and create structure like this:
a=001 b=005 c=02 d=3000.00 e=28
I want to do it with fscanf, but my problem it's that in this line don't exist spaces, and thus, I don't know what is the format that I need to write in fscanf(..)
I've seen this and still I don't understand how to do it.

Did I understand correctly, you are reading in a fixed-field line?
You could try
unsigned int a, b, c, e;
float d;
fscanf(file, "%3u%3u%2u%7f%2u", &a, &b, &c, &d, &e);
printf("a=%u b=%u c=%u d=%f e=%u", a, b, c, d, e);

Related

Extracting between two strings with sed until the first occurrence

Can someone explain why I am getting different outcome while using the same command? I have 2 c++ files:
1st one(ax.h)
int iseven(int a);
int sum(float a, int b);
2nd one(ax.cpp)
//Definicija
int iseven(int a) {
if (a%2==0){
return 1;
else
return 0;}
float sum(float a, int b) {
return a + (float)b;
}
Why is it when I use the command:
sed -n '/iseven/,/}/p' ax.cpp
My output only does what I want and it print the text between iseven and the first occurrence of} ->
int iseven(int a) {
if (a%2==0){
return 1;
else
return 0;}
But when I use the same command(sed -n '/iseven/,/)/p' ax.h) on my 1st file(ax.h), it prints everything out:
int iseven(int a);
int sum(float a, int b);
And what I was expecting was int iseven(int a);
When you use
/start/,/end/ as the address in a sed command, it starts searching for end on the next line after the match of start. So you can't use a pattern range when the end pattern is on the same line as the start pattern.
You could just use /iseven/p to match just those lines in ax.h, unless you need to allow for declarations to be spread over multiple lines. If you need to handle both single-line and multi-line declarations, there might be a way using the hold space, but I'm not an advanced enough sed user to show how to do it.

signed and unsigned fixed point multiplication in SystemVerilog

I have a specific question and a request for more general guidance.
My question is what is the cleanest way to multiply a signed number by an unsigned number in SystemVerilog?
Below is a little test code that illustrates the problem. 'a' is the unsigned number. 'b' is the signed number. In order to produce a correct signed result SystemVerilog seems to require that both operands of the multiplication be signed. To make that work here I had to add an extra '0' to the front of the unsigned number to make it a valid signed number. I'm thinking there must be a cleaner way.
More generally, I am just getting started doing fixed point math in SystemVerilog. In VHDL there is very concrete syntax and even a standard package to support signed and unsigned fixed point math, with rounding, etc. Is there something like that in the SystemVerilog world?
Thanks,
module testbench ();
localparam int Wa = 8;
localparam int Wb = 8;
logic [Wa-1:0] a; // unsigned
logic [Wa:0] a_signed; // signed word with extra bit to hold unsigned number.
logic [Wb-1:0] b; // signed
logic [Wa+Wb-1:0] c; // result
localparam clk_period = 10;
assign a_signed = {1'b0, a};
assign c = $signed(a_signed)*$signed(b);
initial begin
a = +5;
b = +10;
#(clk_period*1);
$display("Hex: a=%x,b=%x, c=%x; Dec: a=%d, b=%d, c=%d", a, b, c, a, $signed(b), $signed(c));
a = +5;
b = -10;
#(clk_period*1);
$display("Hex: a=%x,b=%x, c=%x; Dec: a=%d, b=%d, c=%d", a, b, c, a, $signed(b), $signed(c));
a = +255;
b = +10;
#(clk_period*1);
$display("Hex: a=%x,b=%x, c=%x; Dec: a=%d, b=%d, c=%d", a, b, c, a, $signed(b), $signed(c));
a = +255;
b = -10;
#(clk_period*1);
$display("Hex: a=%x,b=%x, c=%x; Dec: a=%d, b=%d, c=%d", a, b, c, a, $signed(b), $signed(c));
$stop;
end
endmodule
system verilog rules say
If any operand is unsigned, the result is unsigned, regardless of the operator
Propagate the type and size of the expression (or self-determined subexpression) back down to the context-determined operands of the expression
When propagation reaches a simple operand as defined in 11.5, then that operand shall be converted to the propagated type and size.
So, in other words, when you multily signed and unsigned numbers, the expression type will be determined as unsigned. This will be propagated back to operands and all signed will be treated as unsigned as well.
so, your result will be identical to the one of multiplying two unsigned numbers. So, the cleanest way, if you need signed result, is to convert all operands to signed number.
You will also need an extra bit in your operands to have a place for the sign bit. Otherwise 255 will be treated as -1 in 8-bit sign conversion.

Merging function declarations

I have following requirement in my project,
qualifier1 foo(int a, int b, int c); -- In header file header_1.h
qualifier2 foo(int a, int b, int c); -- In header file header_2.h
Where, qualifier1 is not equal to qualifier2.
Can I document the function declaration as shown below using Doxygen,
qualifier1 qualifier2 foo(int a, int b, int c);
Thanks.
INPUT_FILTER option with sed utility solved my problem as shown,
INPUT_FILTER = "sed -e 's/_qualifier1_ foo/_qualifier1_ _qualifier2_ foo/"
** Note the \ (escape) character use to avoid unknown character errors.

How to correctly format function calls in emacs

Does anybody know how to remove unwanted whitespace between function parameters.
for example I have a badly formatted function
foo ( int a , int b );
and I would like to get
foo (int a, int b);
I am looking for somethink similar to clean-ups or colon and brace hanging in CC mode. Ideally the correction is done when typing or pressing tab.
Thanks!
Using query-replace-regexp:
M-C-S 5( +Enter(Enter!
M-C-S 5+,Enter,Enter!
M-C-S 5+)Enter)Enter!

Scanf missed line

I wrote a test program which should take in a 3x3 matrix of characters and output the entered matrix. However, I have to enter 4 lines in order for the program to produce the corresponding matrix. I have looked up problems on the scanf function, but none of the solutions I tried seemed to work...Could you help me out with this?
My code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char a[3][3];
int i,j;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
scanf("%c",&a[i][j]);
}
scanf("\n");
}
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
printf("%c",a[i][j]);
}
printf("\n");
}
system("PAUSE");
return(0); }
scanf("%c",...) get the whitespaces and the \n. You can solve it in many ways:
If you read like a b c
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
scanf("%c",&a[i][j]);
cin.get(); //Get the spaces after each character and the \n at the end of each line
}
}
or you can simple use cin (read char/string inputs with scanf is always a problem)
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
cin >> a[i][j];
}
}
if you are reading like abc, you only have to substitute your scanf("\n") for a cin.get()
#João Menighin's answer surely works. If you want to avoid c++, this would work:
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
scanf(" %c",&a[i][j]);
}
}
Although it would ignore ALL whitespace: both abc and a b c would be interpreted to be equivalent.
try adding a white space in your scanf right after the "
scanf(" %c",&a[i][j]);
I had the same problem in a two-dimension matrix and it worked for me.
I have no idea why though!!! I just spent 1 hour in front of my laptop trying different things...
Have tried your and IT WORKED. Although, I did make a few changes per comments:
#include <stdio.h> // added, but that shouldn't matter
main()
{
char a[3][3];
int i,j;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
scanf("%c",&a[i][j]);
}
//scanf("\n"); // not necessary, see below
}
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
printf(" %c",a[i][j]);
}
printf("\n");
}
return(0);
}
Compiled and ran this code on Eclipse/Microsoft C Compiler and entered series of characters followed by enter.
abcdefghi
a b c
d e f
g h i
The point of confusion might be that scanf pulls the data from a console buffer. Typically, (although you can work around this) that buffer is returned to your program when you press enter. Also, the format specifier of %c also accepts blanks. Thus, I tried a second run with the following input and output.
a b c d e
a b
c
d e
You can tell the spaces were read and stored as well as the letters.
Hope this helps.