Tag: string

sscanf直到它达到逗号

我正在尝试扫描字符串中的单词和数字,如下所示:“ hello,world,I,287876,6.0 ”< – 此字符串存储在char数组中(字符串)我需要做的就是将事情分开并将它们分配给不同的变量,这样就好了 char a = “hello” char b = “world” char c = “I” unsigned long d = 287876 float e = 6.0 我知道常规scanf在到达空白区域时停止从stdin读取。 所以我一直在想,有可能让sscanf在达到“,” (逗号)时停止阅读 我一直在探索图书馆,找到sscanf的格式,只读字母和数字。 我找不到这样的东西,也许我应该再看一次。 有帮助吗? 提前致谢 :)

使用%s打印字符串会打印错误数据

我正在使用fread函数从文件读取字符串然后打印字符串。 我将字符串定义为具有LONGNUM大小(预定义值)的字符数组。 我每次读1个字节1个字节的大小。 当打印字符串时:printf(“读取的字符串是:%s \ n”,缓冲区); 输出是:读取的字符串是b我没有得到它,为什么在stringi的最后得到这个值? 当打印字符串时:printf(“读取的字符串是:%c \ n”,缓冲区[0]); 我得到没有值的写输出。 请解释我原因。 代码是 #include #define LONGNUM 1 void main(int argc,char *argv[]) { FILE * src; FILE * dst; int num; char buffer[LONGNUM]; // argc is the number of the elements in argv // the first argv element is the name of the program if(argc < […]

字符串到Arduino中的const char *?

我有一个变量tweet ,它是一个字符串,它在一开始就有一个字符我要剪掉。 所以我想要做的是使用strstr()来删除它。 这是我的代码: tweet = strstr(tweet, “]”); 但是,我收到此错误: cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to ‘char’ strstr(const char*, const char*) 所以我的想法是将tweet转换为char。 我该怎么做呢?

参数列表是否将字符串引号传递给C中的exec命令?

我正在使用execvp为命令grep -l night *执行一个新进程。 这是我的代码: char * argument[5]; char keyword[] = “night”; argument[0] = (char *) malloc (sizeof(char)*25); argument[1] = (char *) malloc (sizeof(char)*25); argument[2] = (char *) malloc (sizeof(char)*25); argument[3] = (char *) malloc (sizeof(char)*25); argument[4] = (char *) malloc (sizeof(char)*25); argument[0] = “grep”; argument[1] = “-l”; strcpy(argument[2],keyword); argument[3] = “*”; argument[4] = NULL; […]

C中的strcasecmp返回156而不是0,任何想法为什么?

我有以下代码: printf(“num: %d\n”, strcasecmp(buf, “h\n”)); 当我尝试插入不同的字母时,我得到以下结果: a: -7 g: -1 i: 1 j: 2 h: 156 H: 156 当buf等于H或h时, strcasecmp不应该返回0吗? 任何想法为什么它返回156? 我需要弄清楚如何检查用户是否键入H或h 。 谢谢! 编辑:我正在通过以下方式读取buf: read(0, buf, MAXBUFLEN);

迭代char数组和打印字符

我试图在变量中打印每个char。 我可以通过更改为printf(“Value: %d\n”, d[i]);打印ANSI字符编号printf(“Value: %d\n”, d[i]); 但我没有真正打印字符串字符本身。 我在这做错了什么? #include #include #include int main(int argc, char *argv[]) { int len = strlen(argv[1]); char *d = malloc (strlen(argv[1])+1); strcpy(d,argv[1]); int i; for(i=0;i<len;i++){ printf("Value: %s\n", (char)d[i]); } return 0; }

从文本文件中读取字符串和整数

假设我有一个文件看起来像这样 51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers 77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers 31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping 52.03 12.02 12.04 22.00 31.98 61.97 Big Bank 44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping 93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs 我想用fscanf读取这个文件字,将数字放在浮点数组中,将字放在字符串数组中。 但是,经过几个小时的艰苦思考,我仍然无法弄清楚如何解决这个问题。 void […]

LuaJit FFI从C函数返回字符串到Lua?

说我有这个C函数: __declspec(dllexport) const char* GetStr() { static char buff[32] // Fill the buffer with some string here return buff; } 而这个简单的Lua模块: local mymodule = {} local ffi = require(“ffi”) ffi.cdef[[ const char* GetStr(); ]] function mymodule.get_str() return ffi.C.GetStr() end return mymodule 如何从C函数中获取返回的字符串作为Lua字符串: local mymodule = require “mymodule” print(mymodule.get_str())

使用安全function将字符串添加到C中的字符串

我想将文件名复制到字符串并将“.cpt”附加到它。 但我无法使用安全函数(strcat_s)执行此操作。 错误:“字符串未终止!”。 我确实设置’\ 0’,如何使用安全function解决这个问题? size = strlen(locatie); size++; nieuw = (char*)malloc(size+4); strcpy_s(nieuw, size, locatie); nieuw[size] = ‘\0’; strcat_s(nieuw, 4, “.cpt”); // <– crash puts(nieuw);

使用ncurses在C中打印Unicode字符

我必须使用ncurses在C中绘制一个方框; 首先,为简单起见,我定义了一些值: #define RB “\e(0\x6a\e(B” (ASCII 188,Right bottom, for example) 我已经使用gcc编译,通过Ubuntu,使用-finput-charset=UTF-8标志。 但是,如果我尝试使用addstr或printw进行打印,我会得到hexa代码。 我做错了什么?