小编Dav*_*eer的帖子

UNIX shell脚本中的浮点算法

如何在shell脚本中使用1.503923等浮点数进行算术运算?浮点数作为字符串从文件中提取.该文件的格式如下:

1.5493482,3.49384,33.284732,23.043852,2.2384...
3.384,3.282342,23.043852,2.23284,8.39283...
.
.
.
Run Code Online (Sandbox Code Playgroud)

这是一些我需要工作的简化示例代码.一切都很好,直到算术.我从文件中拉出一行,然后从该行中提取多个值.我认为这会减少搜索处理时间,因为这些文件非常庞大.

# set vars, loops etc.

while [ $line_no -gt 0 ]
do
    line_string=`sed -n $line_no'p' $file_path`  # Pull Line (str) from a file
    string1=${line_string:9:6}                   # Pull value from the Line
    string2=${line_string:16:6}
    string3=...
    .
    .
    .
    calc1= `expr $string2 - $string7` |bc -l     # I tried these and various
    calc2= ` "$string3" * "$string2" ` |bc -l    # other combinations
    calc3= `expr $string2 - $string1`
    calc4= "$string2 + $string8" |bc
    .
    . …
Run Code Online (Sandbox Code Playgroud)

unix math floating-point shell

10
推荐指数
3
解决办法
3万
查看次数

C中的变量文件名中的%d

制作记录数据的程序.我希望它在每次达到文件空间的设置限制(以字节为单位)时以递增的整数开始一个新文件.

一切正常,直到我使用:

file_name = ("Data%d.bin",fileIncrement);
Run Code Online (Sandbox Code Playgroud)

代替:

file_name = "Data.bin";
Run Code Online (Sandbox Code Playgroud)

当我替换上面的内容时,软件不再写入文件.

这是有问题的功能:

    char *file_name;
    static int fileSize = 0;  //bytes
    static int fileIncrement = 1;

    //Set file name
    if (fileSize == 0)
    {
        file_name = ("Data%d.bin",fileIncrement);
        ++fileIncrement;
    }

    fileSize += size;

    if (fileSize >= 1024)
    {
        fileSize = 0;
    }

    // Write to file system
    ...
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

我最初尝试使用时间戳,但这不起作用并将其恢复到基础.

c

0
推荐指数
2
解决办法
207
查看次数

标签 统计

c ×1

floating-point ×1

math ×1

shell ×1

unix ×1