首先,我有一个功能,我有unsigned char*类型存储的标志.例如,Unicode中每个符号Abcdef!?为[65 0] [98 0] [99 0] [100 0] [101 0] [102 0] [33 0] [63 0]2个字节.
当我使用for(unsigned char i=0; i<17; i++) printf("%c", pointer[i]);一切正常时,它显示Abcdef!?.但是当我使用printf("%s" pointer);它时,它只给我,A而不是别的.你能告诉我为什么吗?
我已经开始研究使用C的命令处理,但我遇到了这个C程序的问题.它正在执行ls命令之前.
Gcc信息:
gcc version 6.2.1 20161124 (Debian 6.2.1-5)
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("Is command processor available?\n");
if (system(NULL))
{
printf("Command processor available!\n");
}
else
{
printf("Command processor not available!\n");
exit(1);
}
printf("Executing command ls");
i=system("ls");
printf("Returned value is: %d.\n",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我所说的代码就是这条特定的代码:
printf("Executing command: ls");
Run Code Online (Sandbox Code Playgroud)
如果使用该段代码运行程序,则输出为:
Is command processor available?
Command processor is available
systemProcessing systemProcessing.c
Executing command: lsReturned value is: 0.
Run Code Online (Sandbox Code Playgroud)
它在实际被告知之前执行命令
但是当我使用新行"\n"完成代码时,其输出符合预期:
Is command processor available?
Command processor is …Run Code Online (Sandbox Code Playgroud) 如果我想填充一个字符串,我可以使用这样的东西:
https://play.golang.org/p/ATeUhSP18N
package main
import (
"fmt"
)
func main() {
x := fmt.Sprintf("%+20s", "Hello World!")
fmt.Println(x)
}
Run Code Online (Sandbox Code Playgroud)
+ always print a sign for numeric values;
guarantee ASCII-only output for %q (%+q)
- pad with spaces on the right rather than the left (left-justify the field)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想动态更改打击垫大小,我怎么能传递该值?
我的第一位客人是:
x := fmt.Sprintf("%+%ds", 20, "Hello World!")
Run Code Online (Sandbox Code Playgroud)
但我明白了:
%ds%!(EXTRA int=20, string=Hello World!)
Run Code Online (Sandbox Code Playgroud)
有没有办法在不创建自定义填充函数的情况下执行此操作,可能会使用for循环向左或向右添加空格:
for i := 0; i < n; i++ {
out += str
}
Run Code Online (Sandbox Code Playgroud) 我想知道符号中使用的符号是什么printf.我试过谷歌搜索但找不到合适的名字
这里是使用Unicode BOTTOM SQUARE BRACKET字符U + 23B5表示的问题:
printf("?%d\n", br);
Run Code Online (Sandbox Code Playgroud)
(这是为了显示手写代码的样子;该字符不会出现在实际代码中.)
我有功课,我必须创建一个C程序,从最小到最大排序5个数字.我知道如何使用函数和if语句(使用>=和<=)轻松编程.
然而,美中不足的是,我只允许使用printf和scanf,所以我必须要计算所有的>=和<=内部的printf.我不允许使用三元运算符.
我一直在努力.所以我试着只排序3个数字,我仍然无法通过排序最小的数字.它只是保持打印1.
// This printf is just trying to figure out the smallest number from 3 numbers provided but it keeps printing 1.
printf("The ascending order of the numbers are: %d ",
(
(num1 <= num2 && num1 <= num3) || // Checking if num1 is the smallest
(num2 <= num1 && num2 <= num3) || // Checking if num2 is …Run Code Online (Sandbox Code Playgroud) 所以我认为我可以打印出这样的内部十六进制float:
const auto foo = 13.0F;
const auto bar = reinterpret_cast<const unsigned char*>(&foo);
printf("0x%02hhX%02hhX%02hhX%02hhX\n", bar[0], bar[1], bar[2], bar[3]);
Run Code Online (Sandbox Code Playgroud)
这输出:
0x00005041
但是当我查看调试器时,它报告的十六进制foo是:
0x003EF830
有人可以帮助我理解为什么这不起作用以及我需要做些什么来使它工作?
我有点像C的初学者,所以请原谅任何粗鲁的错误!
我试图用printf()打印一些格式化的值,但它在输出的中间开始换行,即使没有'\n'!问题出在442行的list_topic函数中.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
typedef struct
{
int t_id;
char t_name[35];
char date_creation[30];
char last_post_date[30];
int sub_users;
int num_posts;
char t_desc[500];
} topic;
typedef struct
{
int u_type;
int t_maxreach;
int u_sub_topic[15];
char u_name[16];
char u_pass[16];
} utilizador;
void instalar();
void menu_inicial();
void menu_admin();
void menu_topic();
void menu_user();
void login();
void options();
void topic_gest();
void user_gest();
void stats();
void new_topic();
void alt_topic();
void rmv_topic();
void list_topic();
void new_user(); …Run Code Online (Sandbox Code Playgroud) 我在C中的printf函数有问题.虽然缓冲被禁用,但它不打印输出:
setbuf(stdout, NULL);
Run Code Online (Sandbox Code Playgroud)
和
setvbuf(stdout, NULL, _IONBF, 0);
Run Code Online (Sandbox Code Playgroud)
我也在使用fflush(stdout);,但它仍然无效.
这是确切的代码:
int setup(){
//...
printf("Setup successful\n");
fflush(stdout);
return 0;
}
int main(int argc, char *argv[]){
setbuf(stdout, NULL);
setvbuf(stdout, NULL, _IONBF, 0);
setup();
//...
)
Run Code Online (Sandbox Code Playgroud)
如果信息有帮助; 我在Linux(raspberry Pi)上.
提前致谢!
TL; DR:我想通过左边的给定字符将列表的每个字符串填充到给定的大小.我想快点.请参阅下面的代码并举例说明
我有很大的字符串向量,包含......很好的东西,但最大(已知)字符数.我希望通过将Zero's留给给定大小来完成thoose字符串(优于char的最大数量)
假设:
c("yop",NA,"1234567","19","12AN","PLOP","5689777")
Run Code Online (Sandbox Code Playgroud)
假设客观大小为10,我希望:
[1] "0000000yop" NA "0001234567" "0000000019" "00000012AN" "000000PLOP" "0005689777"
Run Code Online (Sandbox Code Playgroud)
结果,尽可能快.
我试着写自己的,但它不是很快......你能帮助我加快速度吗?我有数十亿的待办事项.
这是我的实际代码:
library(purrr)
zero_left <- function(field,nb){
map2_chr(
map(abs(nb-nchar(field)),~ rep("0",.x)),
field,
~ paste0(c(.x,.y),collapse=""))
}
trial <- c("yop","1234567","19","12AN","PLOP","5689777")
zero_left(trial,10)
Run Code Online (Sandbox Code Playgroud)
这段代码甚至不处理NA案例...但没有它它可行,但太慢了.
我想sprintf在R中使用带有可变数量参数的函数。是否可以将这些参数捆绑在一起形成字符向量或列表,以避免sprintf单独提供这些参数?
一个例子将阐明:
base_string = "(1) %s, (2) %s, (3) %s"
sprintf(base_string, "foo", "bar", "baz") # this works
sprintf(base_string, c("foo", "bar", "baz")) # this doesn't work
Run Code Online (Sandbox Code Playgroud)
在Python中,我可以使用
base_string = "(1) %s, (2) %s, (3) %s"
base_string % ("foo", "bar", "baz")
Run Code Online (Sandbox Code Playgroud)