标签: printf

为什么Perl在退出之前不打印最后一个文本?

我的代码不会在" exit;" 之前运行最后一行,我不知道为什么.我之前试图增加printf $fh一条线exit,但这也没有用; 它不会打印任何一行.除了退出前的最后一个打印语句外,其他所有内容都打印正常.

任何线索为什么会这样?或者更好的是,如何修复或解决它?

    if($i>0 && $i<3)
    {
        printf $fh "Partial match found.\n";
        if($matched_uid){printf $fh "UID #: ".$arguid." exists.\n";}
        if($matched_id){printf $fh "ID #: ".$argid." exists.\n";}
        if($matched_uname){printf $fh "Username: ".$arguname." exists.\n";}
        printf $fh "Aborting."; 
        exit;
    }
Run Code Online (Sandbox Code Playgroud)

编辑:
我复制的部分代码包含此内容

select $fh; $| = 1; #set auto flush
Run Code Online (Sandbox Code Playgroud)

也许这就是我的结果无法复制的原因....

perl printf exit

4
推荐指数
3
解决办法
2252
查看次数

如何使用变量设置以fprintf'%s'格式输出的字符数?

我需要为文件写一个可变数量的字符.例如,假设我要打印3个字符."TO"会将"TO"打印到文件中."LongString of Characters"会将"Lon"打印到文件中.

我怎样才能做到这一点?(字符数在另一个变量中定义).我知道这可能是fprintf(文件,"%10s",字符串),但是10是预定义的

c printf file

4
推荐指数
1
解决办法
5623
查看次数

在C中写入文件和控制台

我正在尝试编写一个允许我写入控制台和C文件的函数.

我有以下代码,但我意识到它不允许我附加参数(如printf).

#include <stdio.h>

int footprint (FILE *outfile, char inarray[]) {
    printf("%s", inarray[]);
    fprintf(outfile, "%s", inarray[]);
}

int main (int argc, char *argv[]) {

    FILE *outfile;
    char *mode = "a+";
    char outputFilename[] = "/tmp/footprint.log";
    outfile = fopen(outputFilename, mode);

    char bigfoot[] = "It Smells!\n";
    int howbad = 10;

    footprint(outfile, "\n--------\n");

    /* then i realized that i can't send the arguments to fn:footprints */
    footprint(outfile, "%s %i",bigfoot, howbad); /* error here! I can't send bigfoot and howbad*/

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我被困在这里 有小费吗?对于我想要发送到函数的参数:footprints,它将包含字符串,字符和整数. …

c printf stdio

4
推荐指数
1
解决办法
1万
查看次数

与printf成千上万的分隔符

如何在一个千位分隔符(如逗号)printf?

例:

printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated 
printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated
Run Code Online (Sandbox Code Playgroud)

更新这是我原来的:

foreach(array_keys($totals) as $key){
        if(is_numeric($totals[$key])){
            $totals[$key] = number_format($totals[$key],2);
        }
    }

    echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='4'>Totals:</td>";
    echo "<td class='number'>{$totals['Bought']}</td>";
    echo "<td class='number'>{$totals['Sold']}</td>";
    echo "<td class='number'>{$totals['Fees']}</td>";
    echo "<td class='number'>{$totals['Realized']}</td>";
    echo "<td class='number'>{$totals['Net']}</td>";
    echo "<td colspan='3'>{$totals['EOD Price']}</td>";
    echo "</tr>
        </tfoot>";
Run Code Online (Sandbox Code Playgroud)

我希望它能像:

echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='3'>Totals:</td>";
    printf("<td class='number'>%d</td>", $totals['Bought']) ;
    printf("<td class='number'>%d</td>", $totals['Sold']) ;
    printf("<td class='number'>%.2f</td>", $totals['Fees']) ;
    printf("<td class='number'>%.2f</td>", $totals['Realized']) ; …
Run Code Online (Sandbox Code Playgroud)

php printf number-formatting

4
推荐指数
2
解决办法
7573
查看次数

如何在sprintf()中获得追加模式

看到这段代码:

int main() {
    char a[50];
    FILE *fp;

    fp = fopen("test1.txt", "w");

    sprintf(a,"jigar %d \n", 3);
    fprintf(fp,"jigar %d \n", 3);

    sprintf(a,"patel %d \n", 2);
    fprintf(fp,"patel %d \n", 2);
    printf("%s", a);
}
Run Code Online (Sandbox Code Playgroud)

在这里,使用fprintf,我可以写入文件

jigar 3 
patel 2 
Run Code Online (Sandbox Code Playgroud)

我想要的相同功能在哪里我打印在一个char缓冲区.

但使用sprintf给我缓冲

patel 2 
Run Code Online (Sandbox Code Playgroud)

我有这么多这样的打印,我想在一个char缓冲区中添加,然后我需要将它返回给应用程序,以便如何以最简单和最快的方式获取它?

c string printf

4
推荐指数
1
解决办法
9901
查看次数

fprintf和vfprintf在C++中有什么区别?

我无法找到以下问题的答案,而且我遇到了与功能有关的一些问题.

我的主要编程是在C#中完成的,在学习时从未真正学过C++,但在我目前的工作中,我还要做一些C++编程.

大多数C++编程都是由一名前员工完成的,他还创建了一个日志功能.

偶尔这个函数会导致错误(访问冲突) - 这不会向用户显示,但我在通过调试器运行代码时看到它.

当错误发生时,它指向这行代码:

vfprintf( LogFile, fmt, va );
Run Code Online (Sandbox Code Playgroud)

然后我仔细研究了之前和之后的代码,并将上面的代码置于上下文中,代码是:

void FileLog( char *fmt, ... )
{
  va_list       va;
  struct  time  t;
  struct  date  d;
  long          clk;
  static int    ReEntrant = 0;

  if( FileLogEnabled == false )
    return;

  ReEntrant++;
  if( ReEntrant > 1 )
    return;

  if( LogFile == NULL )
    LogFile = fopen( LogFileName, "a+" );
  if( LogFile != NULL )
  {
    gettime( &t );
    getdate( &d );
    fprintf( LogFile, "\n%d-%02d-%02d %2d:%02d:%02d.%02d0> ", d.da_year, d.da_mon, d.da_day, t.ti_hour, …
Run Code Online (Sandbox Code Playgroud)

c c++ printf access-violation

4
推荐指数
1
解决办法
7059
查看次数

在浮点bash printf上填充零

我希望在bash中使用填充零打印浮点数来填充范围%5.3f.我知道printf函数.

我的问题如下:

printf "0%5.3f\n" 3.00
Run Code Online (Sandbox Code Playgroud)

按预期返回

03.000
Run Code Online (Sandbox Code Playgroud)

但是这条线

printf "0%5.3f\n" 23.00
Run Code Online (Sandbox Code Playgroud)

相反

023.000 
Run Code Online (Sandbox Code Playgroud)

这当然不是我想要的.

有什么建议吗?

precision bash printf

4
推荐指数
1
解决办法
5713
查看次数

在C中的printf中,什么是未引用的`PRIu32`?

我正在查看以下代码:

#include <stdio.h>
#include <inttypes.h>

int main()
{
    uint32_t total = 0;

    printf("\tTotal: %"PRIu32"\n\n", total);
    return total;
}
Run Code Online (Sandbox Code Playgroud)

如何PRIu32适应printf语法?我的意思是,我可以猜测,Iu32可能意味着"整数无符号32位".但是,我不确定找到哪种形式man 3 printf可以容纳引号之外的变量 - 以及如何将其推广到引号外的其他语句.

c printf uint32

4
推荐指数
1
解决办法
5166
查看次数

这种printf格式是否可行(左对齐符号,零符号)

我正在尝试使用printf来获得以下编程输出:

- 20
- 15
- 10
-  5
   0
+  5
+ 10
+ 15
+ 20
Run Code Online (Sandbox Code Playgroud)

关键规格是:

  • 字段宽度始终为4
  • 标志始终是左对齐的
  • 这个数字总是正确的
  • 零没有迹象

到目前为止,我还没有能够提出一个能给我预期结果的printf语句.我最接近的是:

for(int i = -20; i <= 20; i+=5)
{
    printf("%-+4d \n", i);
}
Run Code Online (Sandbox Code Playgroud)

产生:

-20                                                                                                                              
-15                                                                                                                              
-10                                                                                                                              
-5                                                                                                                               
+0                                                                                                                               
+5                                                                                                                               
+10                                                                                                                              
+15                                                                                                                              
+20                                                                                                                              
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,而不必做任何繁琐的字符串操作?

c string formatting printf string-formatting

4
推荐指数
1
解决办法
214
查看次数

Using a #define in a formatted printf to define precision

#include <stdio.h>
#define PREC 4

int main()
{
    long l = 12;
    printf("%PRECld", l);
}
Run Code Online (Sandbox Code Playgroud)

我将如何使用它PREC来定义所需的精度?即预期的产出是0012。目前正在印刷%PRECld。

c printf

4
推荐指数
1
解决办法
71
查看次数