我有一本字典,键和值都是字符串.我想打印每个键值对在它自己的行上,键左对齐,值右对齐.
Key1 Test
Key2 BlahBlah
Run Code Online (Sandbox Code Playgroud)
等等...
在PHP中执行此操作的最佳方法是什么?也许是一个聪明的方法与printf?
_vscprintf在Android上不可用.也vsprintf(NULL, fmt, ap)不起作用(产生seg故障),所以似乎无法计算vsnprintf成功所需的缓冲区大小?
Android 源代码表明__android_log_print函数只是使用vsnprintf将字符串截断为1024 ...
你如何处理这种情况?
我一直在将一些代码从C++转换为C#.我对C#API缺乏了解并不能让我找到相当于fprintf的东西.我基本上要做的是编写一个帮助类来将信息记录到文件中.到目前为止,我已经定义了以下类.如果有人看到不寻常的东西,请告诉我."Log"方法目前仅记录字符串.我不知道这是否是最好的方法.无论如何,我想将一些数字转换为转储到日志文件中.在C++中,我有fprintf进行转换.我怎样才能在C#中实现类似的东西?
fprintf(file, "Wheel1: %f \t Wheel2: %f \t Dist: %f, Wheel0, Wheel1, TotalDist);
Run Code Online (Sandbox Code Playgroud)
public class Logger
{
private string strPathName = string.Empty;
private StreamWriter sw = null;
/// <summary>
/// Constructor
/// </summary>
/// <param name="prefix"></param>
public Logger(string prefix)
{
DateTime datet = DateTime.Now;
// Format string
if (string.IsNullOrEmpty(prefix))
{
prefix += "_";
}
else
{
prefix = "";
}
strPathName = "Log_" + prefix + datet.ToString("MM_dd_hhmmss") + ".log";
if (File.Exists(strPathName) == true)
{
FileStream fs = new FileStream(strPathName, FileMode.OpenOrCreate, …Run Code Online (Sandbox Code Playgroud) 我尝试在C中的服务器应用程序中打印客户端端口.但是我得到客户端端口的负数,有什么奇怪的行为: - /任何人都知道哪里可能有问题?
导致问题的部分代码:
struct sockaddr_in client_address;
int chosenPort = (int) ntohl(client_address.sin_port);
pritf("Client port is %d, chosenPort");
Run Code Online (Sandbox Code Playgroud)
我得到像-2121400320这样的端口.
我正在使用问题printf,fgets因为在我的代码printf之前写的是fget,但它没有运行,它在运行后fgets运行.
enum { max_string = 127 };
static char string[max_string+1] = "";
int main( int argc, char ** argv ) {
printf("Type a String: ");
fgets(string, max_string, stdin);
printf("The String is %s\n", string);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在学习C的过程中一直在疯狂地研究.我一直在调试一个C程序,我认为这里有一些重大问题.现在我有关键问题.我制作了一个虚拟程序,在一个语句中打印两个字符串,如下所示:
#include<stdio.h>
int main(int argc, char* argv[])
{
char *herp = "Derp";
char *derp = "Herp";
printf("Herp %s Derp %s\n", herp, derp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这按预期打印出来.我明白了
Herp Derp Derp Herp
Run Code Online (Sandbox Code Playgroud)
所以,我想,让我通过做类似的事情调试我自己的程序.我的程序中的以下行
printf("word is: %s and jumbled word is: %s\n", word, jumbleWord);
Run Code Online (Sandbox Code Playgroud)
应打印出类似的东西
Word is: word and jumbled word is: dowr
Run Code Online (Sandbox Code Playgroud)
但它打印出类似的东西
and jumbled word is: dowr
Run Code Online (Sandbox Code Playgroud)
输出的第一部分在哪里?我需要能够在同一行上打印这两个进行调试.此外,这样的陈述不起作用这一事实告诉我,真的很奇怪的事情正在发生,我秃头撕裂了我的头发.正如我的链接帖子所示,我最终想比较这些字符串值,但是如果printf()不能正常工作,我怎么能这样做呢?
我发布了下面的整个程序,这样你就可以看到一切都在发生.我只是在学习如何使用指针.当我想混淆一个单词并且效果不好时,我最初有两个指针指向相同的内存!所以我修复了它,并用我需要的单词获得了两个独立的内存空间.现在,我只是无法打印它们.鉴于以下代码,这一切都有意义:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_WORD_LENGTH 25
//Define global variables
int numWords;
//Preprocessed Functions
void jumblegame();
void readFile(char *[]);
void …Run Code Online (Sandbox Code Playgroud) 我想在C中编写一个简单的main函数,它接收两行字符串输入并在屏幕上打印它们.这是我写的代码:
int main()
{
char a[100];
char b[100];
printf("Enter the first string:\n");
fgets(a,100,stdin);
printf("Enter the second string:\n");
fgets(b,100,stdin);
printf("\n\n THE FIRST STRING IS: %S\n\n THE SECOND STRING IS:%S",a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我收到此错误消息:
gcc -g -Wall PN52.c -o myprog
PN52.c: In function ‘main’:
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘char *’ [-Wformat]
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我正在以这种方式读取二进制模式的文件:
fread(&pcap_header ,sizeof(pcap_hdr_t ), 1 , pFile);
fread(&pcaprec_header ,sizeof(pcaprec_hdr_t ), 1 , pFile);
fread(ðer_header ,sizeof(ethernet_hdr_t ), 1 , pFile);
fread(&ip_header ,sizeof(ip_hdr_t ), 1 , pFile);
fread(&tcp_header ,sizeof(tcp_header_t ), 1 , pFile);
fread(&modbus_header ,sizeof(modbus_header_t ), 1 , pFile);
Run Code Online (Sandbox Code Playgroud)
而上面的所有变量都被定义为旁边的'sizeof()'函数中的类型.我正在尝试提取文件的特定部分并将其打印出来.这部分的大小是16位,它的定义如下:
uint16_t ip_len;
Run Code Online (Sandbox Code Playgroud)
在'ip_hdr_t'类型中.我查看了WireShark并看到了这16位的实际值,它是0x0034.
我尝试了两件事:
printf("size of ip header: %hu\n", ip_header.ip_len); 打印13312(好像位的值是0x3400)uint8_t ip_len;
uint8_t ip_len2;
并单独打印:
printf("size of ip header: %hu\n", ip_header.ip_len);
printf("size of ip header: %hu\n", ip_header.ip_len2);这次我收到了输出:ip header的大小:0(0x00)ip header的大小:52(0x34),这是正确的答案.
我的问题是:第一次印刷出了什么问题?
谢谢
所以我有一个长度为10的字符串.我想要打印4-6 [包括两者]的字母,我想跨越特定长度L的输出,数字位于右侧.
例如
如果我有原始字符串123456789,则以下代码应以后续方式显示.
printf(Original);
printf(from 4-6, over 5 spaces)
Run Code Online (Sandbox Code Playgroud)
输出:
123456789
456
Run Code Online (Sandbox Code Playgroud)
那是456分布在5个空格并且向右缩进.
我怎么用C做?
编辑1:如果我的宽度不恒定怎么办,并且我有一个变量,它决定了宽度.类似地,子串的长度是变量.现在有办法吗?
我能做点什么吗
printf("%%d.%ds",width,precision,expression);
Run Code Online (Sandbox Code Playgroud) 我需要在数字前打印一些前导空格和零,以便输出如下:
00015
22
00111
8
126
Run Code Online (Sandbox Code Playgroud)
在这里,我需要leading spaces在数字even和leading zero时间打印odd
我是这样做的:
int i, digit, width=5, x=15;
if(x%2==0) // number even
{
digit=log10(x)+1; // number of digit in the number
for(i=digit ; i<width ; i++)
printf(" ");
printf("%d\n",x);
}
else // number odd
{
digit=log10(x)+1; // number of digit in the number
for(i=digit ; i<width ; i++)
printf("0");
printf("%d\n",x);
}
Run Code Online (Sandbox Code Playgroud)
有没有捷径可以做到这一点?
printf ×10
c ×8
string ×2
android ×1
android-ndk ×1
c# ×1
class ×1
debugging ×1
equivalent ×1
fgets ×1
formatting ×1
php ×1
port ×1
uint16 ×1