在C中,使用printf
中的stdio.h
即可轻松打印到标准输出。
但是,如何打印到stderr?我们可以使用fprintf
来实现它,但它的语法似乎很奇怪。也许我们可以使用printf
打印到stderr?
答案 0 :(得分:126)
语法与printf
几乎相同。使用printf
,您可以提供字符串格式及其内容,即:
printf("my %s has %d chars\n", "string format", 30);
使用fprintf
它是相同的,除了现在您还要指定要打印的位置:
File *myFile;
...
fprintf( myFile, "my %s has %d chars\n", "string format", 30);
或者在你的情况下:
fprintf( stderr, "my %s has %d chars\n", "string format", 30);
答案 1 :(得分:25)
示例:
printf("%s", "Hello world\n"); // "Hello world" on stdout (using printf)
fprintf(stdout, "%s", "Hello world\n"); // "Hello world" on stdout (using fprintf)
fprintf(stderr, "%s", "Stack overflow!\n"); // Error message on stderr (using fprintf)
答案 2 :(得分:5)
你知道sprintf
吗?它与fprintf
基本相同。第一个参数是目标(fprintf
的情况下的文件,即stderr
),第二个参数是格式字符串,其余的是通常的参数。
答案 3 :(得分:5)
#include<stdio.h>
int main ( ) {
printf( "hello " );
fprintf( stderr, "HELP!" );
printf( " world\n" );
return 0;
}
$ ./a.exe
HELP!hello world
$ ./a.exe 2> tmp1
hello world
$ ./a.exe 1> tmp1
HELP!$
stderr通常是无缓冲的,通常是stdout。这可能导致像这样奇怪的输出,这表明代码以错误的顺序执行。它不是,只是stdout缓冲区还没有被刷新。 重定向或管道流当然不会看到这种交错,因为它们通常只能看到stdout或stderr的输出。
虽然最初stdout和stderr都进入控制台,但两者都是分开的,可以单独重定向。
答案 4 :(得分:4)
如果您不想修改当前代码并仅修改调试用途。
添加此宏:
<control:CarouselView x:Name="carouselView">
<control:CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="46*"></RowDefinition>
<RowDefinition Height="0.2*"></RowDefinition>
<RowDefinition Height="53.8*"></RowDefinition>
</Grid.RowDefinitions>
<!-- griglia del carousel, tutta la struttura inizia qui-->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"></ColumnDefinition>
<ColumnDefinition Width="35*"></ColumnDefinition>
<ColumnDefinition Width="45*"></ColumnDefinition>
<ColumnDefinition Width="10*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Image="backBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmePrev" Grid.Column="0"></Button>
<!--<Image Source="backBlueArrow.png" x:Name="backClickArrow" Aspect="AspectFit" Grid.Column="0"></Image>-->
<StackLayout Margin="0,0,10,0" VerticalOptions="Center" HorizontalOptions="End" Grid.Column="1">
<Image HeightRequest="70" Source="{Binding Immagine}" Aspect="AspectFit"></Image>
</StackLayout>
<!-- griglia superiore, riprende i dati tra le barre bianche-->
<Grid Grid.Row="0" Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40*"></RowDefinition>
<RowDefinition Height="60*"></RowDefinition>
</Grid.RowDefinitions>
<Label Text="{Binding Descrizione}" Margin="{Binding Margine}" TextColor="#fff" FontSize="Small" VerticalTextAlignment="End" HorizontalTextAlignment="Start" Grid.Row="0"/>
<Label Text="{Binding Valore}" FontSize="Large" TextColor="#fff" VerticalTextAlignment="Start" HorizontalTextAlignment="Start" Grid.Row="1"/>
<Image Margin="-25,-5,0,0" Source="{Binding ImmagineBatteriaEmoticon}" HeightRequest="50" x:Name="emoticonBattery" Grid.Row="1" VerticalOptions="Start" HorizontalOptions="Start"></Image>
</Grid>
<Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button>
<!--<Image Source="nextBlueArrow.png" x:Name="nextClickArrow" Aspect="AspectFit" Grid.Column="3"></Image>-->
</Grid>
<!-- griglia fare la riga bianca -->
<Grid BackgroundColor="White" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
<!-- griglia inferiore, ritrare i dati dei testi -->
<ScrollView Grid.Row="2">
<Grid Padding="5" Grid.Row="2" x:Name="firstGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65*"></ColumnDefinition>
<ColumnDefinition Width="35*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="0" Text="{Binding Testo1}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="0" Text="{Binding Valore1}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="1" Text="{Binding Testo2}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="1" Text="{Binding Valore2}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="2" Text="{Binding Testo3}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="2" Text="{Binding Valore3}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="3" Text="{Binding Testo4}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="3" Text="{Binding Valore4}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="4" Text="{Binding Testo5}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="4" Text="{Binding Valore5}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="5" Text="{Binding Testo6}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="5" Text="{Binding Valore6}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="6" Text="{Binding Testo7}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="6" Text="{Binding Valore7}"></Label>
<Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="7" Text="{Binding Testo8}"></Label>
<Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="7" Text="{Binding Valore8}"></Label>
</Grid>
<RelativeLayout Grid.Row="2" x:Name="secondGrid" BackgroundColor="Fuchsia" HeightRequest="50" WidthRequest="100">
<Label Text="ciao"></Label>
</RelativeLayout>
</ScrollView>
</Grid>
<!--<Label Text="{Binding Testo}" />-->
</DataTemplate>
</control:CarouselView.ItemTemplate>
</control:CarouselView>
如果要回滚,请将$radios = $radios->map(function ($radio, $key) {
$coordinates = $radio->coordinates;
$coordinates = $coordinates->filter(function($coordinate, $key) {
// return true or false;
});
$radio['coordinates'] = $coordinates;
return $radio;
});
更改为#define printf(args...) fprintf(stderr, ##args)
//under GCC
#define printf(args...) fprintf(stderr, __VA_ARGS__)
//under MSVC
。
这对调试很有帮助,但这不是一个好习惯。
答案 5 :(得分:-1)
要打印您的上下文,您可以编写如下代码:
FILE *fp;
char *of;
sprintf(of,"%s%s",text1,text2);
fp=fopen(of,'w');
fprintf(fp,"your print line");